~ubuntu-branches/debian/sid/kmess/sid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/***************************************************************************
                          emoticonspage.cpp -  description
                             -------------------
    begin                : Sun Dev 11 2005
    copyright            : (C) 2005 by Diederik van der Boor
    email                : "vdboor" --at-- "codingdomain.com"
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "emoticonspage.h"

#include "../dialogs/addemoticondialog.h"
#include "../currentaccount.h"
#include "../emoticonmanager.h"
#include "../kmessdebug.h"

#include <QDir>
#include <QListWidget>
#include <QPixmap>

#include <KMessageBox>
#include <KStandardDirs>


#ifdef KMESSDEBUG_EMOTICONS
  #define KMESSDEBUG_EMOTICONS_SETTINGS
#endif



/**
 * Constructor
 *
 * @param parent  Parent widget
 */
EmoticonsPage::EmoticonsPage( QWidget* parent )
  : QWidget(parent)
  , Ui::EmoticonsPage()
  , emoticonTheme_(0)
{
  // First setup the user interface
  setupUi( this );

  // Connect the signals we'll need for the Custom Emoticons tab
  connect( addEmoticonButton_,    SIGNAL(                clicked()                 ),
           this,                    SLOT(  showAddEmoticonDialog()                 ) );
  connect( renameEmoticonButton_, SIGNAL(                clicked()                 ),
           this,                    SLOT(   renameCustomEmoticon()                 ) );
  connect( removeEmoticonButton_, SIGNAL(                clicked()                 ),
           this,                    SLOT(   removeCustomEmoticon()                 ) );
  connect( customEmoticonsView_,  SIGNAL(          itemActivated(QListWidgetItem*) ),
           this,                    SLOT(   renameCustomEmoticon()                 ) );
  connect( customEmoticonsView_,  SIGNAL(            itemChanged(QListWidgetItem*) ),
           this,                    SLOT(  customEmoticonRenamed(QListWidgetItem*) ) );
  connect( customEmoticonsView_,  SIGNAL(   itemSelectionChanged()                 ),
           this,                    SLOT( customEmoticonSelected()                 ) );

  // the other properties of the widget are initialized
  // in the loadSettings() method
}



/**
 * Destructor
 */
EmoticonsPage::~EmoticonsPage()
{
  if( emoticonTheme_ != 0 )
  {
#ifdef KMESSDEBUG_EMOTICONS_SETTINGS
    kDebug() << "Deleting temporary theme.";
#endif
    delete emoticonTheme_;
  }
}



/**
 * Rename the currently selected custom emoticon
 *
 * @param item  The changed item
 */
void EmoticonsPage::customEmoticonRenamed( QListWidgetItem *item )
{
  QString newName( item->text() );

#ifdef KMESSDEBUG_EMOTICONS_SETTINGS
  kDebug() << "Renaming emoticon '" << renamedItemName_ << "' to '" << newName << "'";
#endif

  // Do nothing if the name ain't changed, or there is no old name (when creating the list)
  if( renamedItemName_.isEmpty() || newName.isEmpty() || renamedItemName_ == newName )
  {
    // Restore the original name if needed
    if( newName.isEmpty() )
    {
      item->setText( renamedItemName_ );
    }

    return;
  }

  // Verify if the shortcut length is within the allowed limit
  if( newName.size() > 7 )
  {
    KMessageBox::sorry( this,
                        i18nc( "Dialog box text",
                               "You can only use 7 characters for the emoticon shortcuts." ),
                        i18nc( "Dialog box title", "Invalid Emoticon Name" ) );

    // Opening the editor on the item again fails here, probably because this
    // method is called as a slot by the editor itself; so only cut down the
    // new name to size (losing data). Stuff sucks.
    newName.truncate( 7 );
  }

  // Check if an emoticon with the new name already exists
  if( emoticonTheme_->contains( newName ) )
  {
    // Don't ask for confirmation if the emoticon isn't valid
    int result;
    if( emoticonTheme_->getEmoticon( newName )->isValid() )
    {
      result = KMessageBox::questionYesNo( this,
                                           i18n( "The emoticon \"%1\" already exists. Do you want to replace it?", newName ),
                                           i18nc( "Dialog box title", "Replace Existing Emoticon" )
                                           );
    }
    else
    {
      result = KMessageBox::Yes;
    }

    // The user doesn't want to overwrite the emoticon, cancel the operation
    if( result != KMessageBox::Yes )
    {
      item->setText( renamedItemName_ );
      renamedItemName_ = QString();
      return;
    }
    else
    {
      emoticonTheme_->removeEmoticon( newName );
    }
  }

  // If no other emoticons with that shortcut exist, update the name
  emoticonTheme_->renameEmoticon( renamedItemName_, newName );

  // Reset the old name for future rename operations
  renamedItemName_ = QString();

  // Update the viewport and the buttons
  customEmoticonSelected();
}



/**
 * Update the Remove Button status when emoticons get selected
 */
void EmoticonsPage::customEmoticonSelected()
{
  bool hasSelection = ( ! customEmoticonsView_->selectedItems().isEmpty() );

  renameEmoticonButton_->setEnabled( hasSelection );
  removeEmoticonButton_->setEnabled( hasSelection );

  // Clear the variable controlling the item rename logic
  renamedItemName_ = QString();
}



/**
 * Load the settings of the dialog
 *
 * @param account  Account instance which settings will be loaded
 */
void EmoticonsPage::loadSettings( const Account *account )
{
  accountHandle_ = account->getHandle();

#ifdef KMESSDEBUG_EMOTICONS_SETTINGS
  kDebug() << "Loading emoticon settings for account " << accountHandle_ << " - Current is " << accountHandle_ << " .";
#endif

  // Read the settings and set current emoticons
  emoticonStyle_ = account->getEmoticonStyle();

  emoticonTheme_ = new EmoticonTheme();
  emoticonTheme_->loadTheme( accountHandle_, true );

  // Update the emoticon themes widget
  updateThemesList();

  // Update the custom emoticons widget
  updateCustomEmoticonView();

#ifdef KMESSDEBUG_EMOTICONS_SETTINGS
  kDebug() << "Done!";
#endif
}



/**
 * Delete the selected custom emoticon from the theme and the view
 */
void EmoticonsPage::removeCustomEmoticon()
{
  const QList<QListWidgetItem *>& selectedItems( customEmoticonsView_->selectedItems() );

  if( selectedItems.isEmpty() )
  {
    return;
  }

  const QListWidgetItem *item = selectedItems.first();

  // Request confirmation
  int result = KMessageBox::questionYesNo( this,
                                       i18n( "Are you sure you want to delete the emoticon \"%1\" ?", item->text() ),
                                       i18nc( "Dialog box title", "Delete Emoticon" ) );
  if( result != KMessageBox::Yes )
  {
    return;
  }

  // Remove the emoticon from the temporary theme, and if that succeeds, from the list
  int itemRow = customEmoticonsView_->row( item );
  if( emoticonTheme_->removeEmoticon( item->text() ) && itemRow >= 0 )
  {
    // Safe, takeItem() returns 0 on failure
    delete customEmoticonsView_->takeItem( itemRow );

    // Forcibly update the buttons status to disable the rename&remove buttons
    customEmoticonSelected();
  }
}



/**
 * Starts in-place renaming of the selected custom emoticon
 */
void EmoticonsPage::renameCustomEmoticon()
{
  QListWidgetItem *item = customEmoticonsView_->currentItem();

  if( item == 0 )
  {
    return;
  }

  // Save the current shortcut of the emoticon, so that we'll know what the
  // original shortcut was when the user renames it
  renamedItemName_ = item->text();

  // Temporarily set the item as editable
  item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable );

  customEmoticonsView_->setFocus();
  customEmoticonsView_->editItem( item );

  // Remove its "Editable" flag
  item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
}



/**
 * Save the settings to the account object
 *
 * @param account  Account instance which settings will be saved
 */
void EmoticonsPage::saveSettings( Account *account )
{
  // Save the changes we've made
  emoticonTheme_->saveTheme();

  // Enable emoticon styles only if there's one selected (the list may be empty)
  if( emoticonThemesList_->currentItem() )
  {
    account->setEmoticonStyle( emoticonThemesList_->currentItem()->text() );
  }
  else
  {
    account->setEmoticonStyle( QString() );
  }

  // Apply the changes to the current account, too
  if( accountHandle_ == CurrentAccount::instance()->getHandle() )
  {
    EmoticonManager::instance()->replaceCustomTheme( emoticonTheme_ );
  }
}



/**
 * Show the Add New Emoticon dialog to create a new custom emoticon
 */
void EmoticonsPage::showAddEmoticonDialog()
{
  AddEmoticonDialog *dialog = new AddEmoticonDialog( emoticonTheme_, this );

  connect( dialog, SIGNAL(            addedEmoticon(QString) ),
           this,     SLOT( updateCustomEmoticonView()        ) );
}



// Force the page onto a specific tab
void EmoticonsPage::switchToTab( int tabIndex )
{
  if( ( tabIndex < 0 ) || tabIndex > tabWidget_->count() )
  {
    kWarning() << "Invalid tab index" << tabIndex << "selected!";
    return;
  }

  tabWidget_->setCurrentIndex( tabIndex );
}



/**
 * Fill the widget which contains all the user's custom emoticons
 */
void EmoticonsPage::updateCustomEmoticonView()
{
  QListWidgetItem *item;

  customEmoticonsView_->clear();

  // Add all emoticons to the list
  const QList<Emoticon*>& emoticons( emoticonTheme_->getEmoticons() );
  foreach( Emoticon *emoticon, emoticons )
  {
    if( ! emoticon->isValid() )
    {
      continue;
    }

    item = new QListWidgetItem( QIcon( emoticon->getPicturePath() ), emoticon->getShortcut(), customEmoticonsView_ );
    item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
  }

  // Force an update of the view's buttons
  customEmoticonSelected();
}



/**
 * Initialize the standard emoticon theme chooser
 */
void EmoticonsPage::updateThemesList()
{
  emoticonThemesList_->clear();

  // Browse all emoticon dirs, to get all emoticons
  const QStringList& themesDirs( KGlobal::dirs()->findDirs("emoticons", "") );

#ifdef KMESSDEBUG_EMOTICONS_SETTINGS
  kDebug() << "Theme dirs: " << themesDirs;
#endif

  QDir themesDir;
  foreach( const QString& themesPath, themesDirs )
  {
    // Browse for all themes in the emoticon dir
    themesDir.setPath( themesPath );

    // Grep all themes in current dir
    const QStringList& themes( themesDir.entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name | QDir::IgnoreCase ) );

    foreach( const QString& themePath, themes )
    {
#ifdef KMESSDEBUG_EMOTICONS_SETTINGS
      kDebug() << "Folder: " << themesPath << " - Theme: " << themePath;
#endif
      // Add the theme to the list, using the directory name; its first valid emoticon is used as a preview
      const QPixmap previewPixmap( EmoticonTheme::getThemeIcon( themesPath + themePath ) );
      new QListWidgetItem( previewPixmap, themePath, emoticonThemesList_ );
    }
  }

  // Highlight the selected emoticon
  QListWidgetItem* item = 0;
  QList<QListWidgetItem*> items( emoticonThemesList_->findItems( emoticonStyle_, Qt::MatchExactly | Qt::MatchCaseSensitive ) );

  if( items.isEmpty() )
  {
    kWarning() << "Current emoticon style was not found, attempting to revert to the default setting.";
    items = emoticonThemesList_->findItems( "KMess-new", Qt::MatchExactly | Qt::MatchCaseSensitive );
  }
  else
  {
    item = items.first();
  }

  // If there is an item, select it
  if( item != 0 )
  {
    emoticonThemesList_->setCurrentItem( item );
  }
  else
  {
    // select the first item otherwise
    emoticonThemesList_->setCurrentRow( 0 );
  }
}



#include "emoticonspage.moc"