text: /**********************************************************************
   * LeechCraft - modular cross-platform feature rich internet client.
   * Copyright (C) 2006-2014  Georg Rudoy
   *
   * Boost Software License - Version 1.0 - August 17th, 2003
   *
   * Permission is hereby granted, free of charge, to any person or organization
   * obtaining a copy of the software and accompanying documentation covered by
   * this license (the "Software") to use, reproduce, display, distribute,
   * execute, and transmit the Software, and to prepare derivative works of the
   * Software, and to permit third-parties to whom the Software is furnished to
   * do so, all subject to the following:
   *
   * The copyright notices in the Software and this entire statement, including
   * the above license grant, this restriction and the following disclaimer,
   * must be included in all copies of the Software, in whole or in part, and
   * all derivative works of the Software, unless such copies or derivative
   * works are solely in the form of machine-executable object code generated by
   * a source language processor.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
   * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
   * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   * DEALINGS IN THE SOFTWARE.
   **********************************************************************/
  
  #include "selfcontact.h"
  #include <algorithm>
  #include <QXmppVCardManager.h>
  #include "clientconnection.h"
  #include "vcarddialog.h"
  #include "accountsettingsholder.h"
  #include "glooxmessage.h"
  
  namespace LeechCraft
  {
  namespace Azoth
  {
  namespace Xoox
  {
  	SelfContact::SelfContact (const QString& fullJid, GlooxAccount *acc)
  	: EntryBase (acc->GetSettings ()->GetJID (), acc)
  	{
  		UpdateJID (fullJid);
  
  		connect (this,
  				SIGNAL (vcardUpdated ()),
  				this,
  				SLOT (handleSelfVCardUpdated ()));
  	}
  
  	IAccount* SelfContact::GetParentAccount () const
  	{
  		return Account_;
  	}
  
  	ICLEntry::Features SelfContact::GetEntryFeatures () const
  	{
  		return FSupportsGrouping | FPermanentEntry | FSelfContact;
  	}
  
  	ICLEntry::EntryType SelfContact::GetEntryType () const
  	{
  		return EntryType::Chat;
  	}
  
  	QString SelfContact::GetEntryName () const
  	{
  		return Account_->GetNick ();
  	}
  
  	void SelfContact::SetEntryName (const QString&)
  	{
  		qWarning () << Q_FUNC_INFO
  				<< "can't set name of self contact";
  	}
  
  	QString SelfContact::GetEntryID () const
  	{
  		return Account_->GetAccountID () + '_' + ".self";
  	}
  
  	QStringList SelfContact::Groups () const
  	{
  		return QStringList (tr ("Self contact"));
  	}
  
  	void SelfContact::SetGroups (const QStringList&)
  	{
  		qWarning () << Q_FUNC_INFO
  				<< "can't set groups of self contact";
  	}
  
  	QStringList SelfContact::Variants () const
  	{
  		auto result = Status2Prio_.keys ();
  		std::sort (result.begin (), result.end (),
  				[this] (const QString& left, const QString& right)
  					{ return Status2Prio_ [left] > Status2Prio_ [right]; });
  		return result;
  	}
  
  	EntryStatus SelfContact::GetStatus (const QString& resource) const
  	{
  		if (resource == Resource_)
  			return Account_->GetState ();
  
  		return EntryBase::GetStatus (resource);
  	}
  
  	IMessage* SelfContact::CreateMessage (IMessage::Type type,
  			const QString& variant, const QString& text)
  	{
  		const auto msg = Account_->CreateMessage (type, variant, text, GetJID ());
  		AllMessages_ << msg;
  		return msg;
  	}
  
  	QList<QAction*> SelfContact::GetActions () const
  	{
  		return EntryBase::GetActions ();
  	}
  
  	void SelfContact::UpdatePriority (const QString& resource, int prio)
  	{
  		Status2Prio_.remove (resource);
  		Status2Prio_ [resource] = prio;
  		emit availableVariantsChanged (Variants ());
  	}
  
  	void SelfContact::RemoveVariant (const QString& resource, bool thisInstance)
  	{
  		if (thisInstance)
  			for (const auto& otherResource : Status2Prio_.keys ())
  				if (otherResource != resource)
  					RemoveVariant (otherResource, false);
  
  		Status2Prio_.remove (resource);
  		CurrentStatus_.remove (resource);
  
  		EntryBase::SetStatus (EntryStatus (SOffline, QString ()),
  				resource,
  				QXmppPresence (QXmppPresence::Unavailable));
  	}
  
  	QString SelfContact::GetJID () const
  	{
  		return BareJID_;
  	}
  
  	void SelfContact::UpdateJID (const QString& fullJid)
  	{
  		ClientConnection::Split (fullJid, &BareJID_, &Resource_);
  
  		emit availableVariantsChanged (Variants ());
  	}
  
  	void SelfContact::handleSelfVCardUpdated ()
  	{
  		Account_->UpdateOurPhotoHash (VCardPhotoHash_);
  	}
  }
  }
  }
  