[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdenetwork/kopete/libkopete
From:       Michaël Larouche <larouche () kde ! org>
Date:       2007-02-23 0:19:14
Message-ID: 1172189954.353173.6015.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 636403 by mlarouche:

Implement "Remove Avatar" in AvatarManager and AvatarSelectorWidget.

Next thing is AvatarSelectorDialog :)



 M  +37 -12    kopeteavatarmanager.cpp  
 M  +19 -4     kopeteavatarmanager.h  
 M  +82 -10    ui/avatarselectorwidget.cpp  
 M  +13 -0     ui/avatarselectorwidget.h  
 M  +1 -1      ui/avatarselectorwidget.ui  


--- trunk/KDE/kdenetwork/kopete/libkopete/kopeteavatarmanager.cpp #636402:636403
@@ -178,10 +178,35 @@
 	return newEntry;
 }
 
-void AvatarManager::remove(Kopete::AvatarManager::AvatarEntry entryToRemove)
+bool AvatarManager::remove(Kopete::AvatarManager::AvatarEntry entryToRemove)
 {
-	Q_UNUSED(entryToRemove);
-	emit avatarRemoved(entryToRemove);
+	// We need name and path to remove an avatar from the storage.
+	if( entryToRemove.name.isEmpty() && entryToRemove.path.isEmpty() )
+		return false;
+	
+	// We don't allow removing avatars from Contact category
+	if( entryToRemove.category & Kopete::AvatarManager::Contact )
+		return false;
+
+	// Delete the image file first, file delete is more likely to fail than config \
group remove. +	if( KIO::NetAccess::del(KUrl(entryToRemove.path),0) )
+	{
+		kDebug(14010) << k_funcinfo << "Removing avatar from config." << endl;
+
+		KUrl configUrl(d->baseDir);
+		configUrl.addPath( UserDir );
+		configUrl.addPath( AvatarConfig );
+
+		KConfigGroup avatarConfig ( KSharedConfig::openConfig( configUrl.path(), \
KConfig::OnlyLocal ), entryToRemove.name ); +		avatarConfig.deleteGroup();
+		avatarConfig.sync();
+
+		emit avatarRemoved(entryToRemove);
+
+		return true;
+	}
+	
+	return false;
 }
 
 void AvatarManager::Private::createDirectory(const KUrl &directory)
@@ -201,32 +226,32 @@
 	// Maybe the image doesn't need to be scaled
 	QImage result = source;
 
-	if( source.width() > 96 || source.height() > 96 )
+	if( result.width() > 96 || result.height() > 96 )
 	{
 		// Scale and crop the picture.
-		result = source.scaled( 96, 96, Qt::KeepAspectRatioByExpanding, \
Qt::SmoothTransformation ); +		result = result.scaled( 96, 96, \
Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation );  // crop image if not \
square  if( result.width() < result.height() )
 			result = result.copy( (result.width()-result.height())/2, 0, 96, 96 );
 		else if( result.width() > result.height() )
 			result = result.copy( 0, (result.height()-result.width())/2, 96, 96 );
 	}
-	else if( source.width() < 32 || source.height() < 32 )
+	else if( result.width() < 32 || result.height() < 32 )
 	{
 		// Scale and crop the picture.
-		result = source.scaled( 96, 96, Qt::KeepAspectRatioByExpanding, \
Qt::SmoothTransformation ); +		result = result.scaled( 96, 96, \
Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation );  // crop image if not \
square  if( result.width() < result.height() )
 			result = result.copy( (result.width()-result.height())/2, 0, 32, 32 );
 		else if( result.width() > result.height() )
 			result = result.copy( 0, (result.height()-result.width())/2, 32, 32 );
 	}
-	else if( source.width() != source.height() )
+	else if( result.width() != result.height() )
 	{
-		if(source.width() < source.height())
-			result = source.copy((source.width()-source.height())/2, 0, source.height(), \
                source.height());
-		else if (source.width() > source.height())
-			result = source.copy(0, (source.height()-source.width())/2, source.height(), \
source.height()); +		if(result.width() < result.height())
+			result = result.copy((result.width()-result.height())/2, 0, result.height(), \
result.height()); +		else if (result.width() > result.height())
+			result = result.copy(0, (result.height()-result.width())/2, result.height(), \
result.height());  }
 
 	return result;
--- trunk/KDE/kdenetwork/kopete/libkopete/kopeteavatarmanager.h #636402:636403
@@ -64,16 +64,28 @@
  * @subsection avatar_management_add Adding an avatar
  * Use the add() method to add an new avatar. For an avatar received from a contact, \
                you
  * should use the contactId for the name. You can either specify a image path or a \
                QImage.
- * AvatarManager will save the QImage on disk.
+ * AvatarManager will save the QImage on disk. When adding an avatar for Contact \
category, + * the "contact" entry need to be filled with a pointer to a \
Kopete::Contact instance. + *
  * @code
 Kopete::AvatarManager::AvatarEntry newEntry;
 newEntry.name = "A new avatar"
-newEntry.path = avatarPath;
-newEntry.category = Kopete::AvatarManager::User;
+newEntry.image = avatarImage;
+newEntry.contact = this;
+newEntry.category = Kopete::AvatarManager::Contact;
 
 Kopete::AvatarManager::self()->add(newEntry);
  * @endcode
  *
+ * If the operation has failed, the resulting AvatarEntry path will be empty.
+ * The following code is a good way to test the success of add() method:
+ * @code
+if( !resultEntry.path.isEmpty() )
+{
+	// Set avatar on server
+}
+ * @endcode
+ *
  * @subsection avatar_management_delete Removing an avatar
  * To remove an avatar, create a new AvatarEntry struct and pass the avatar path 
  * to the struct, it will be used to identify which avatar to remove.
@@ -86,6 +98,9 @@
 Kopete::AvatarManager::self()->remove(entryToRemove);
  * @endcode
  *
+ * @subsection avatar_management_update Updating an avatar
+ * Adding an avatar with the same name will update the previous avatar.
+ *
  * @author Michaël Larouche <larouche@kde.org>
  */
 class KOPETE_EXPORT AvatarManager : public QObject
@@ -169,7 +184,7 @@
 	 * @brief Remove an avatar from the storage
 	 * @param entryToRemove Avatar entry to remove
 	 */
-	void remove(Kopete::AvatarManager::AvatarEntry entryToRemove);
+	bool remove(Kopete::AvatarManager::AvatarEntry entryToRemove);
 
 private:
 	/**
--- trunk/KDE/kdenetwork/kopete/libkopete/ui/avatarselectorwidget.cpp #636402:636403
@@ -36,6 +36,29 @@
 namespace UI
 {
 
+class AvatarSelectorWidgetItem : public QListWidgetItem
+{
+public:
+	AvatarSelectorWidgetItem(QListWidget *parent)
+	 : QListWidgetItem(parent, QListWidgetItem::UserType)
+	{}
+
+	void setAvatarEntry(Kopete::AvatarManager::AvatarEntry entry)
+	{
+		m_entry = entry;
+		setText( entry.name );
+		setIcon( QIcon(entry.path) );
+	}
+
+	Kopete::AvatarManager::AvatarEntry avatarEntry() const
+	{
+		return m_entry;
+	}
+
+private:
+	Kopete::AvatarManager::AvatarEntry m_entry;
+};
+
 class AvatarSelectorWidget::Private
 {
 public:
@@ -51,10 +74,12 @@
 
 	// Connect signals/slots
 	connect(d->mainWidget.buttonAddAvatar, SIGNAL(clicked()), this, \
SLOT(buttonAddAvatarClicked())); +	connect(d->mainWidget.buttonRemoveAvatar, \
SIGNAL(clicked()), this, SLOT(buttonRemoveAvatarClicked()));  \
connect(d->mainWidget.listUserAvatar, SIGNAL(itemClicked(QListWidgetItem*)), this, \
SLOT(listSelectionChanged(QListWidgetItem*)));  \
connect(d->mainWidget.listUserContact, SIGNAL(itemClicked(QListWidgetItem*)), this, \
SLOT(listSelectionChanged(QListWidgetItem*)));  
 	connect(Kopete::AvatarManager::self(), \
SIGNAL(avatarAdded(Kopete::AvatarManager::AvatarEntry)), this, \
SLOT(avatarAdded(Kopete::AvatarManager::AvatarEntry))); \
+	connect(Kopete::AvatarManager::self(), \
SIGNAL(avatarRemoved(Kopete::AvatarManager::AvatarEntry)), this, \
SLOT(avatarRemoved(Kopete::AvatarManager::AvatarEntry)));  
 	// List avatars in lists
 	Kopete::AvatarQueryJob *queryJob = new Kopete::AvatarQueryJob(this);
@@ -93,9 +118,19 @@
 
 		Kopete::AvatarManager::self()->add( newEntry );
 	}
-	else
+}
+
+void AvatarSelectorWidget::buttonRemoveAvatarClicked()
+{
+	// You can't remove from listUserContact, so we can always use listUserAvatar
+	AvatarSelectorWidgetItem *selectedItem = static_cast<AvatarSelectorWidgetItem*>( \
d->mainWidget.listUserAvatar->selectedItems().first() ); +	if( selectedItem )
 	{
-		// TODO
+		if( !Kopete::AvatarManager::self()->remove( selectedItem->avatarEntry() ) )
+		{
+			// TODO: Show error message
+			kDebug(14010) << k_funcinfo << "Removing of avatar failed for unknown reason." << \
endl; +		}
 	}
 }
 
@@ -122,6 +157,49 @@
 	d->addItem(newEntry);
 }
 
+void AvatarSelectorWidget::avatarRemoved(Kopete::AvatarManager::AvatarEntry \
entryRemoved) +{
+	// Same here, avatar can be only removed from listUserAvatar
+	QList<QListWidgetItem *> foundItems = d->mainWidget.listUserAvatar->findItems( \
entryRemoved.name, Qt::MatchContains ); +	if( !foundItems.isEmpty() )
+	{
+		kDebug(14010) << k_funcinfo << "Removing " << entryRemoved.name << " from list." \
<< endl; +
+		int deletedRow = d->mainWidget.listUserAvatar->row( foundItems.first() );
+		QListWidgetItem *removedItem = d->mainWidget.listUserAvatar->takeItem( deletedRow \
); +		delete removedItem;
+
+		int newRow = --deletedRow;
+		if( newRow < 0 )
+			newRow = 0;
+
+		// Select the previous avatar in the list, thus selecting a new avatar
+		// and deselecting the avatar being removed.
+		d->mainWidget.listUserAvatar->setCurrentRow( newRow );
+		// Force update
+		listSelectionChanged( d->mainWidget.listUserAvatar->item(newRow) );
+	}
+}
+
+void AvatarSelectorWidget::listSelectionChanged(QListWidgetItem *item)
+{
+	if( item )
+		d->mainWidget.labelAvatarImage->setPixmap( item->icon().pixmap(96, 96) );
+
+	// I know sender() is evil
+	// Disable Remove Avatar button when selecting an item in listUserContact.
+	// I don't know anyone who will want to remove avatar received from contacts.
+	if( sender() == d->mainWidget.listUserContact )
+	{
+		d->mainWidget.buttonRemoveAvatar->setEnabled(false);
+	}
+	else
+	{
+		d->mainWidget.buttonRemoveAvatar->setEnabled(true);
+	}
+}
+
+
 void AvatarSelectorWidget::Private::addItem(Kopete::AvatarManager::AvatarEntry \
entry)  {
 	kDebug(14010) << k_funcinfo << "Entry(" << entry.name << "): " << entry.category << \
endl; @@ -140,16 +218,10 @@
 		return;
 	}
 
-	QListWidgetItem *item = new QListWidgetItem(listWidget);
-	item->setText( entry.name );
-	item->setIcon( QIcon(entry.path) );
+	AvatarSelectorWidgetItem *item = new AvatarSelectorWidgetItem(listWidget);
+	item->setAvatarEntry(entry);
 }
 
-void AvatarSelectorWidget::listSelectionChanged(QListWidgetItem *item)
-{
-	d->mainWidget.labelAvatarImage->setPixmap( item->icon().pixmap(96, 96) );
-}
-
 } // Namespace Kopete::UI
 
 } // Namespace Kopete
--- trunk/KDE/kdenetwork/kopete/libkopete/ui/avatarselectorwidget.h #636402:636403
@@ -67,6 +67,12 @@
 	
 	/**
 	 * @internal
+	 * Remove Avatar button was clicked
+	 */
+	void buttonRemoveAvatarClicked();
+	
+	/**
+	 * @internal
 	 * Avatar query job was finished
 	 */
 	void queryJobFinished(KJob *job);
@@ -80,6 +86,13 @@
 
 	/**
 	 * @internal
+	 * An avatar has been removed from storage
+	 * @param entryRemoved Avatar entry removed
+	 */
+	void avatarRemoved(Kopete::AvatarManager::AvatarEntry entryRemoved);
+
+	/**
+	 * @internal
 	 * A new item was selected in lists
 	 * @param item new selected QListWidgetItem
 	 */
--- trunk/KDE/kdenetwork/kopete/libkopete/ui/avatarselectorwidget.ui #636402:636403
@@ -138,7 +138,7 @@
         </widget>
        </item>
        <item>
-        <widget class="QPushButton" name="buttonAddAvatar" >
+        <widget class="QPushButton" name="buttonRemoveAvatar" >
          <property name="text" >
           <string>Remove Avatar</string>
          </property>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic