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

emoticonmanager.cpp

/***************************************************************************
                          emoticonmanager.cpp - handles emoticon themes for current account
                             -------------------
    begin                : Tue April 10 2007
    copyright            : (C) 2007 by Valerio Pilo
    email                : amroth@coldshock.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 **************************************************************************/

#include "emoticonmanager.h"

#include <qcolor.h>
#include <qptrlist.h>
#include <qstringlist.h>

#include <kdebug.h>
#include <klocale.h>

#include "emoticon.h"
#include "emoticontheme.h"
#include "kmessdebug.h"
#include "currentaccount.h"

#ifdef KMESSDEBUG_EMOTICONS
#define KMESSDEBUG_EMOTICONS_GENERAL
#endif



// Initialize the singleton instance to zero
EmoticonManager* EmoticonManager::instance_(0);



/**
 * Constructor
 */
00046 EmoticonManager::EmoticonManager()
  : QObject(0, "EmoticonManager")
  , customTheme_(new EmoticonTheme())
  , shouldSaveTheme_(false)
  , standardTheme_(new EmoticonTheme())
{
  // Create the default KMess standard theme, to always have a fallback theme
  if( ! standardTheme_->loadTheme( "KMess-new", false ) )
  {
    kdWarning() << "EmoticonManager::EmoticonManager() - Cannot create standard emoticon theme!" << endl;
  }
  else
  {
    // Change theme and connect its update signal
    connect( standardTheme_, SIGNAL( updated() ), this, SIGNAL( updated() ) );
  }

  // Connect the theme update signal to detect its changes
  connect( customTheme_, SIGNAL( updated() ), this, SIGNAL( updated() ) );
}



/**
 * Destructor
 */
00072 EmoticonManager::~EmoticonManager()
{
  customTheme_->saveTheme();

  delete customTheme_;
  delete standardTheme_;
}



/**
 * Load an account's emoticon themes
 *
 * To load the custom emoticon theme we use the account's handle as the theme name, so every account
 * will get its very own emoticons.
 * Giving this method an account handle is only useful if we're loading the Account Settings for an account
 * different than the currently connected one.
 *
 * @param handle  Optional account email, if given will load the specified account's theme instead of the current one's
 */
00092 void EmoticonManager::connected( QString handle )
{
  QString themeName;

  // The account will be needed in the disconnected() method, to decide whether to save the theme or not
  Account *account = CurrentAccount::instance();
  shouldSaveTheme_ = ! account->isGuestAccount();

  if( handle.isEmpty() )
  {
    // Update the standard emoticons with the user's preferred theme
    slotChangedEmoticonSettings();

    themeName = account->getHandle();
  }
  else
  {
    // Not updating the standard emoticons when loading an account's settings

    themeName = handle;
  }

#ifdef KMESSDEBUG_EMOTICONS_GENERAL
  kdDebug() << "EmoticonManager::connected() - Loading custom emoticon theme of: " << themeName << endl;
#endif

  // When this call fails, it'll give feedback about the cause. No need to do it here too.
  customTheme_->loadTheme( themeName, true );

  emit updated();
}



/**
 * Delete the instance of the emoticon manager
 */
00129 void EmoticonManager::destroy()
{
  delete instance_;
  instance_ = 0;
}



/**
 * Unload the custom emoticon theme of the current account
 */
00140 void EmoticonManager::disconnected()
{
#ifdef KMESSDEBUG_EMOTICONS_GENERAL
  kdDebug() << "EmoticonManager::disconnected() - Destroying custom emoticon theme." << endl;
#endif

  // Only save the theme when the account is not temporary
  if( shouldSaveTheme_ )
  {
    customTheme_->saveTheme();
  }
#ifdef KMESSDEBUG_EMOTICONS_GENERAL
  else
  {
    // NOTE If a guest user adds emoticons, there will be its folder with the relative images.
    // I don't delete nor move them, since it may be a registered account which, for once, has
    // been made not to remember changes.
    kdDebug() << "EmoticonManager::disconnected() - Guest account - do not save the theme." << endl;
  }
#endif


  // Change theme and connect its update signal
  delete customTheme_;
  customTheme_ = new EmoticonTheme();
  connect( customTheme_, SIGNAL( updated() ), this, SIGNAL( updated() ) );

  emit updated();
}



/**
 * Return the picture file names of all emoticons
 */
00175 const QMap<QString,QString>& EmoticonManager::getFileNames( bool getCustomTheme ) const
{
  if( getCustomTheme )
  {
    return customTheme_->getFileNames();
  }
  else
  {
    return standardTheme_->getFileNames();
  }
}



/**
 * Return the search pattern to find emoticons in an HTML document
 */
00192 const QRegExp& EmoticonManager::getHtmlPattern( bool getCustomTheme ) const
{
  if( getCustomTheme )
  {
    return customTheme_->getHtmlPattern();
  }
  else
  {
    return standardTheme_->getHtmlPattern();
  }
}



/**
 * Return the HTML replacement codes for all emoticons in a theme
 */
00209 const QMap<QString,QString>& EmoticonManager::getHtmlReplacements( bool small, bool getCustomTheme ) const
{
  if( getCustomTheme )
  {
    return customTheme_->getHtmlReplacements( small );
  }
  else
  {
    return standardTheme_->getHtmlReplacements( small );
  }
}



/**
 * Return the search pattern to find emoticons in a text
 */
00226 const QRegExp& EmoticonManager::getPattern( bool getCustomTheme ) const
{
  if( getCustomTheme )
  {
    return customTheme_->getPattern();
  }
  else
  {
    return standardTheme_->getPattern();
  }
}



/**
 * Return one replacement code for the given emoticon
 */
00243 const QString & EmoticonManager::getReplacement( const QString &code, bool small, bool getCustomTheme ) const
{
  if( getCustomTheme )
  {
    return customTheme_->getReplacement( code, small );
  }
  else
  {
    return standardTheme_->getReplacement( code, small );
  }
}



/**
 * Return the replacement codes for all emoticons in a theme
 */
00260 const QMap<QString,QString>& EmoticonManager::getReplacements( bool small, bool getCustomTheme ) const
{
  if( getCustomTheme )
  {
    return customTheme_->getReplacements( small );
  }
  else
  {
    return standardTheme_->getReplacements( small );
  }
}



/**
 * Return a pointer to an emoticons theme
 */
00277 EmoticonTheme *EmoticonManager::getTheme( bool getCustomTheme )
{
  if( getCustomTheme )
  {
    return customTheme_;
  }
  else
  {
    return standardTheme_;
  }
}



/**
 * Return the path where the theme's emoticons are located
 */
00294 const QString& EmoticonManager::getThemePath( bool getCustomTheme )
{
  if( getCustomTheme )
  {
    return customTheme_->getThemePath();
  }
  else
  {
    return standardTheme_->getThemePath();
  }
}



/**
 * Return a singleton instance of the emoticon manager
 */
00311 EmoticonManager* EmoticonManager::instance()
{
  // If the instance is null, create a new emoticon manager and return that.
  if ( instance_ == 0 )
  {
    instance_ = new EmoticonManager();
    connect( CurrentAccount::instance(), SIGNAL(changedEmoticonSettings()     ),
             instance_,                    SLOT(slotChangedEmoticonSettings() ) );
  }
  return instance_;
}



/**
 * Replace the emoticons in the text with the urls to the emoticon pngs.
 */
00328 void EmoticonManager::parseEmoticons( QString &text, bool small, bool useCustomTheme )
{
  int pos = 0;

  EmoticonTheme *usedTheme;

  if( useCustomTheme )
  {
    usedTheme = customTheme_;
  }
  else
  {
    usedTheme = standardTheme_;
  }

  const QRegExp               &pattern      = usedTheme->getPattern();
  const QMap<QString,QString> &replacements = usedTheme->getReplacements( small );

  while( pos != -1 )
  {
    pos = pattern.search(text, pos);

    if( pos != -1 )
    {
      // Found a emoticon
      QString code = text.mid(pos, pattern.matchedLength());
      if( replacements.contains(code) )
      {
        QString replacement = replacements[code];
        text = text.replace(pos, pattern.matchedLength(), replacement);
        pos += replacement.length();
      }
      else
      {
        // No replacement found, preserve original code, move to next
        pos++;
      }
    }
  }
}



/**
 * Replace the custom emoticon theme with a new one
 */
00374 void EmoticonManager::replaceCustomTheme( EmoticonTheme *newTheme )
{
#ifdef KMESSDEBUG_EMOTICONS_GENERAL
  kdDebug() << "EmoticonManager::replaceCustomTheme() - Replacing custom emoticon theme with a new one." << endl;
#endif

  delete customTheme_;
  customTheme_ = new EmoticonTheme( *newTheme );

  // Connect the update signal
  connect( customTheme_, SIGNAL( updated() ), this, SIGNAL( updated() ) );

  emit updated();
}



/**
 * The emoticon theme has changed, update our settings
 *
 * The theme won't be replaced with the emoticon definitions from the theme file,
 * since KMess is an MSN-only client and other clients don't support these special emoticons.
 * Instead, the current emoticon theme is kept, and the code tries to detect which
 * filenames the theme uses for certain emoticon codes.
 */
00399 void EmoticonManager::slotChangedEmoticonSettings()
{
#ifdef KMESSDEBUG_EMOTICONS_GENERAL
  kdDebug() << "EmoticonManager::slotChangedEmoticonSettings() - Emoticon theme changed, updating emoticons." << endl;
#endif

  bool isLoaded;

  // Find out which theme is used now.
  const QString &currentStyle = CurrentAccount::instance()->getEmoticonStyle();

  // And load it
  isLoaded = standardTheme_->loadTheme( currentStyle, false );

  // Warn if something has gone wrong
  if( ! isLoaded )
  {
    kdWarning() << "EmoticonManager::slotChangedEmoticonSettings() - Cannot set standard emoticon theme '" << currentStyle << "'." << endl;
    return;
  }

  emit updated();
}



#include "emoticonmanager.moc"

Generated by  Doxygen 1.6.0   Back to index