Logo Search packages:      
Sourcecode: kmess version File versions  Download package

void AddEmoticonDialog::slotButtonClicked ( int  button ) [private, slot]

Adds the new emoticon to the theme

First it copies the user-selected picture to the custom theme's folder, then adds it to the theme itself; finally it closes the dialog.

Definition at line 257 of file addemoticondialog.cpp.

References EmoticonTheme::addEmoticon(), EmoticonTheme::contains(), EmoticonTheme::getThemePath(), EmoticonTheme::removeEmoticon(), EmoticonTheme::renameEmoticon(), and EmoticonTheme::saveTheme().

{
  bool isEditing = isEditing_;
  isEditing_ = false;

  // If the Cancel button has been pressed, close without saving
  if( button != Ok )
  {
    KDialog::slotButtonClicked( button );
    return;
  }

  QImageReader emoticonData;
  QString shortcut, pictureName;

#ifdef KMESSDEBUG_EMOTICONS_ADDING
  kmDebug() << "OK button was pressed!";
#endif

  shortcut = shortcutEdit_->text();
  pictureName = pictureEdit_->text();

  // Check if the shortcut is really changed
  if( preselectedShortcut_ == shortcut && isEditing )
  {
    accept();
    return;
  }

  emoticonData.setFileName( pictureName );
  QString format( emoticonData.format() );

  if( format.isEmpty() || shortcut.isEmpty() )
  {
    kmWarning() << "Shortcut or picture is not valid!";
    return;
  }

  // If the emoticon does already exist in the theme, ask if you want to replace it.
  if( theme_->contains( shortcut ) )
  {
#ifdef KMESSDEBUG_EMOTICONS_ADDING
    kmDebug() << "Asking for overwrite.";
#endif

    int result = KMessageBox::questionYesNo( this,
                                             i18n( "The emoticon \"%1\" already exists. Do you want to replace it?", shortcut ),
                                             i18n("Add New Emoticon") );
    if ( result == KMessageBox::Yes )
    {
      theme_->removeEmoticon( shortcut );
    }
    else
    {
      return;
    }
  }

  // Create any missing directories.
  QDir dir;
  dir.mkpath( theme_->getThemePath() );

  if( isEditing )
  {
#ifdef KMESSDEBUG_EMOTICONS_ADDING
    kmDebug() << "Editing emoticon: shortcut changed from " << preselectedShortcut_ << " to " << shortcut;
#endif
    theme_->renameEmoticon( preselectedShortcut_, shortcut );
    theme_->saveTheme();

    emit changedEmoticon( shortcut );

    accept();
    return;
  }

  // Access the original picture file details
  QFileInfo pictureInfo( pictureName );

  // For the most common image formats, keep the original filetype; otherwise, save the image as a PNG. This increases portability
  // between 3rd-party clients and ensures that MSN will always be able to read our emoticons.
  if( format == "gif" || format == "png" || format == "jpg" )
  {
    // Just copy over the image

    // Get handles for the original picture and the new emoticon
    QFile originalPicture( pictureName );
    QFile newThemePicture( theme_->getThemePath() + pictureInfo.fileName() );

#ifdef KMESSDEBUG_EMOTICONS_ADDING
    kmDebug() << "Copying original picture" << pictureName << "to theme as" << newThemePicture.fileName();
#endif

    // Open the two images
    if( ! originalPicture.open( QIODevice::ReadOnly ) )
    {
      kmWarning() << "Couldn't open source file: " << originalPicture.fileName();
      return;
    }

    if( ! newThemePicture.open( QIODevice::WriteOnly ) )
    {
      kmWarning() << "Couldn't open destination file: " << newThemePicture.fileName();
      return;
    }

    // Copy the file contents
    newThemePicture.write( originalPicture.readAll() );
    newThemePicture.close();
    originalPicture.close();

    // Quick validity check
    if( newThemePicture.size() != originalPicture.size() )
    {
      kmWarning() << "File sizes do not match: "
                  <<           originalPicture.fileName() << " (" << originalPicture.size() << ")"
                  << " != " << newThemePicture.fileName() << " (" << newThemePicture.size() << ")" << endl;
      return;
    }
  }
  else
  {
    // Save the loaded picture as a PNG image.

#ifdef KMESSDEBUG_EMOTICONS_ADDING
    kmDebug() << "Recoding original picture" << pictureName << "(which format was" << format << ") to theme as" << pictureInfo.completeBaseName() << ".png";
#endif

    emoticonData.read().save( theme_->getThemePath() + pictureInfo.completeBaseName() + ".png", "png" );
  }


#ifdef KMESSDEBUG_EMOTICONS_ADDING
   kmDebug() << "Adding new emoticon: " << shortcut << ".";
#endif

  // We've just created a file whose name is like "file.name.png", so give to the theme a name "file.name" so it can guess
  // the file type on its own.
  theme_->addEmoticon( pictureInfo.completeBaseName(), QStringList( shortcut ) );
  theme_->saveTheme();

  // If a shortcut has been preselected, signal that we've added that emoticon.
  // If it hasn't, just send the new shortcut.
  if( ! preselectedShortcut_.isEmpty() )
  {
    shortcut = preselectedShortcut_;
  }

  // Signal to anyone interested that we've added this emoticon
  emit addedEmoticon( shortcut );

  accept();
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index