~ubuntu-branches/ubuntu/lucid/kmess/lucid

« back to all changes in this revision

Viewing changes to kmess/settings/accountswidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter, Sven Boden, Harald Sitter
  • Date: 2008-02-11 23:32:07 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080211233207-zdif7j0ikcnzvbbm
Tags: 1.5-0ubuntu1
[ Sven Boden ]
* Upgrade kmess from v1.4.3 to v1.5 (LP: #191092)

[ Harald Sitter ]
* Raised Standards-Version to 3.7.3
* Outsourced the Homepage to an own entry
* Fixed debian/watch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          accountswidget.cpp  -  description
 
3
                             -------------------
 
4
    copyright            : (C) 2004 by Madelman
 
5
                           (C) 2006 by Diederik van der Boor
 
6
    email                : mkb137b@hotmail.com
 
7
                           "vdboor" --at-- "codingdomain.com"
 
8
 ***************************************************************************/
 
9
 
 
10
/***************************************************************************
 
11
 *                                                                         *
 
12
 *   This program is free software; you can redistribute it and/or modify  *
 
13
 *   it under the terms of the GNU General Public License as published by  *
 
14
 *   the Free Software Foundation; either version 2 of the License, or     *
 
15
 *   (at your option) any later version.                                   *
 
16
 *                                                                         *
 
17
 ***************************************************************************/
 
18
 
 
19
#include "accountswidget.h"
 
20
 
 
21
#include <qwidget.h>
 
22
#include <qcheckbox.h>
 
23
#include <qcombobox.h>
 
24
#include <qfileinfo.h>
 
25
#include <qfile.h>
 
26
#include <qimage.h>
 
27
#include <qlabel.h>
 
28
#include <qlayout.h>
 
29
#include <qlineedit.h>
 
30
#include <qpixmap.h>
 
31
 
 
32
#include <kmessagebox.h>
 
33
#include <kdebug.h>
 
34
#include <kdeversion.h>
 
35
 
 
36
#include <kpassdlg.h>
 
37
#include <krun.h>
 
38
#include <kurllabel.h>
 
39
 
 
40
#include <kaction.h>
 
41
#include <klistbox.h>
 
42
#include <kpopupmenu.h>
 
43
#include <kpushbutton.h>
 
44
#if KDE_IS_VERSION(3,4,0)
 
45
  #include <kpixmapregionselectordialog.h>
 
46
#else
 
47
#endif
 
48
 
 
49
#include <kio/netaccess.h>
 
50
#include <klocale.h>
 
51
#include <kmdcodec.h>
 
52
#include <kstandarddirs.h>
 
53
#include <kfiledialog.h>
 
54
 
 
55
 
 
56
 
 
57
/**
 
58
 * Constructor
 
59
 */
 
60
AccountsWidget::AccountsWidget( QWidget* parent,  const char* name, WFlags fl )
 
61
: AccountsWidgetInterface( parent, name, fl ),
 
62
  hasTempImage_(false)
 
63
{
 
64
 
 
65
  // Find the default image
 
66
  KStandardDirs *dirs   = KGlobal::dirs();
 
67
  QString defaultImage  = dirs->findResource( "data", "kmess/pics/kmesspic.png"  );
 
68
 
 
69
  // Load the image in the X-Server:
 
70
  defaultPixmap_ = QPixmap(defaultImage);
 
71
  customPixmap_  = defaultPixmap_;
 
72
 
 
73
  // Create the "Change..." button actions
 
74
#if KDE_IS_VERSION(3,4,0)
 
75
  KPopupMenu  *browsePopup   = new KPopupMenu( this, "browsemenu" );
 
76
  KAction     *browseSimple  = new KAction( i18n("Browse..."),                "folder_open", 0, this, "browsesimple" );
 
77
  KAction     *browseResize  = new KAction( i18n("Browse and crop image..."), "editcut",     0, this, "browseresize" );
 
78
  connect( browseSimple, SIGNAL(activated()), this,  SLOT(pictureBrowseSimple() ) );
 
79
  connect( browseResize, SIGNAL(activated()), this,  SLOT(pictureBrowseResize() ) );
 
80
 
 
81
  // Plug the items into the browse menu button
 
82
  browseSimple->plug( browsePopup );
 
83
  browseResize->plug( browsePopup );
 
84
 
 
85
  // Assign the new popup to the button and detach old connection
 
86
  browseButton_->setPopup( browsePopup );
 
87
  disconnect( browseButton_, SIGNAL( clicked() ) );
 
88
#endif
 
89
 
 
90
  // Add the items to the initial status combo box
 
91
  QStringList statuses;
 
92
  statuses << i18n("Online")
 
93
           << i18n("Away")
 
94
           << i18n("Be Right Back")
 
95
           << i18n("Busy")
 
96
           << i18n("Out to Lunch")
 
97
           << i18n("On the Phone")
 
98
           << i18n("Invisible");
 
99
  initialStatus_->insertStringList( statuses );
 
100
}
 
101
 
 
102
 
 
103
 
 
104
/**
 
105
 * Destructor
 
106
 */
 
107
AccountsWidget::~AccountsWidget()
 
108
{
 
109
  // Clean up the temp file.
 
110
  if(hasTempImage_)
 
111
  {
 
112
    QFile::remove(tempImageFile_);
 
113
  }
 
114
 
 
115
  // no need to delete child widgets, Qt does it all for us
 
116
}
 
117
 
 
118
 
 
119
 
 
120
// Return the filename of the currently selected image.
 
121
QString AccountsWidget::getPictureFileName() const
 
122
{
 
123
  if( hasTempImage_ )
 
124
  {
 
125
    return tempImageFile_;
 
126
  }
 
127
  else
 
128
  {
 
129
    return imageFile_;
 
130
  }
 
131
}
 
132
 
 
133
 
 
134
 
 
135
/**
 
136
 * Load the dialog settings.
 
137
 */
 
138
void AccountsWidget::loadSettings(Account *account, bool isCurrentAccount)
 
139
{
 
140
  bool showImage;
 
141
 
 
142
  // Read settings
 
143
  myHandle_      = account->getHandle();
 
144
  imageFile_     = account->getCustomImagePath();
 
145
  tempImageFile_ = account->getCustomImagePath() + ".tmp";
 
146
  showImage      = account->getShowImage();
 
147
 
 
148
  // Load default properties
 
149
  friendlyNameEdit_ ->setText( account->getFriendlyName() );
 
150
  handleEdit_       ->setText( myHandle_ );
 
151
  passwordEdit_     ->setText( QString::null );
 
152
 
 
153
  autologinCheckBox_      ->setChecked( account->getUseAutologin()   );
 
154
 
 
155
 
 
156
  // Make sure the drop down list matches the user's initial status
 
157
  int item;
 
158
  QString status = account->getInitialStatus();
 
159
  if( status == "AWY" )
 
160
  { // Change status to AWAY
 
161
    item = 1;
 
162
  }
 
163
  else if( status == "BRB" )
 
164
  { // Change status to BE RIGHT BACK
 
165
    item = 2;
 
166
  }
 
167
  else if( status == "BSY" )
 
168
  { // Change status to BUSY
 
169
    item = 3;
 
170
  }
 
171
  else if( status == "LUN" )
 
172
  { // Change status to OUT TO LUNCH
 
173
    item = 4;
 
174
  }
 
175
  else if( status == "PHN" )
 
176
  { // Change status to ON THE PHONE
 
177
    item = 5;
 
178
  }
 
179
  else if( status == "HDN" )
 
180
  { // Change status to INVISIBLE
 
181
    item = 6;
 
182
  }
 
183
  else
 
184
  { // Change status to ONLINE
 
185
    item = 0;
 
186
  }
 
187
  initialStatus_->setCurrentItem( item );
 
188
 
 
189
 
 
190
  // The friendly name edit should only be enabled if this is the current account, and the passport account is verified;
 
191
  // the "register" text and link make sense only if this is not the current account;
 
192
  // the "verify" text and link should only be visible if the currently logged in account has not been verified.
 
193
  if( isCurrentAccount )
 
194
  {
 
195
    if( account->isVerified() )
 
196
    {
 
197
      friendlyNameEdit_ ->setEnabled( true );
 
198
      registerLabel_    ->hide();
 
199
      registerButton_   ->hide();
 
200
      verifyLabel_      ->hide();
 
201
      verifyButton_     ->hide();
 
202
    }
 
203
    else
 
204
    {
 
205
      friendlyNameEdit_ ->setEnabled( false );
 
206
      registerLabel_    ->hide();
 
207
      registerButton_   ->hide();
 
208
      verifyLabel_      ->show();
 
209
      verifyButton_     ->show();
 
210
    }
 
211
  }
 
212
  else
 
213
  {
 
214
    friendlyNameEdit_ ->setEnabled( false );
 
215
    registerLabel_    ->show();
 
216
    registerButton_   ->show();
 
217
    verifyLabel_      ->hide();
 
218
    verifyButton_     ->hide();
 
219
  }
 
220
 
 
221
  // When the account is a guest account, offer a method to upgrade to a real account.
 
222
  if( account->isGuestAccount() )
 
223
  {
 
224
    // Disable settings that require a real account which is saved in the config file.
 
225
    rememberMeToggled(false);
 
226
  }
 
227
  else
 
228
  {
 
229
    // Hide the upgrade function
 
230
    rememberCheckbox_->hide();
 
231
  }
 
232
 
 
233
  // Load the custom pixmap if we have one.
 
234
  customPixmap_ = QPixmap( imageFile_ );
 
235
 
 
236
  if(customPixmap_.isNull())
 
237
  {
 
238
    // Failed to load
 
239
    customPixmap_ = defaultPixmap_;
 
240
    imageFile_    = QString::null;
 
241
  }
 
242
 
 
243
  // Load the pixmap
 
244
  pictureLabel_->setPixmap(customPixmap_);
 
245
 
 
246
  // Set the "Show Image" checkbox
 
247
  // Run the event manually this time to update the GUI.
 
248
  noPictureCheckbox_->setChecked( ! showImage );
 
249
  showImageToggled( ! showImage );
 
250
}
 
251
 
 
252
 
 
253
 
 
254
// Show the normal browse dialog to change display picture
 
255
void AccountsWidget::pictureBrowseSimple()
 
256
{
 
257
  selectImage( false );
 
258
}
 
259
 
 
260
 
 
261
 
 
262
// Show the browse dialog to change display picture and then allow the user to crop it
 
263
void AccountsWidget::pictureBrowseResize()
 
264
{
 
265
  selectImage( true );
 
266
}
 
267
 
 
268
 
 
269
 
 
270
/**
 
271
 * Save the settings in the account object.
 
272
 */
 
273
void AccountsWidget::saveSettings( Account *account )
 
274
{
 
275
  QString   password;
 
276
  QString   imageName     = 0;
 
277
  QString   tempImageName = 0;
 
278
  QString   initialStatus;
 
279
  QDir      imageFolder;
 
280
  bool      showImage;
 
281
  bool      success;
 
282
 
 
283
  // Read settings
 
284
  showImage = ! noPictureCheckbox_->isChecked();
 
285
 
 
286
  // When the account is a guest account, see if the user wanted to "upgrade" it.
 
287
  if( account->isGuestAccount() )
 
288
  {
 
289
    if( rememberCheckbox_->isChecked() )
 
290
    {
 
291
      account->setGuestAccount(false);
 
292
    }
 
293
  }
 
294
 
 
295
  // Update the settings
 
296
  account->setLoginInformation( handleEdit_->text(), friendlyNameEdit_->text(), passwordEdit_->password() );
 
297
  account->setUseAutologin( autologinCheckBox_->isChecked() );
 
298
  account->setShowImage( showImage );
 
299
 
 
300
  // Update the initial status
 
301
  initialStatus = initialStatus_->currentText();
 
302
  if( initialStatus == i18n("Away") )
 
303
  { // Change status to AWAY
 
304
    initialStatus = "AWY";
 
305
  }
 
306
  else if( initialStatus == i18n("Be Right Back") )
 
307
  { // Change status to BE RIGHT BACK
 
308
    initialStatus = "BRB";
 
309
  }
 
310
  else if( initialStatus == i18n("Busy") )
 
311
  { // Change status to BUSY
 
312
    initialStatus = "BSY";
 
313
  }
 
314
  else if( initialStatus == i18n("Invisible") )
 
315
  { // Change status to INVISIBLE
 
316
    initialStatus = "HDN";
 
317
  }
 
318
  else if( initialStatus == i18n("Out to Lunch") )
 
319
  { // Change status to OUT TO LUNCH
 
320
    initialStatus = "LUN";
 
321
  }
 
322
  else if( initialStatus == i18n("On the Phone") )
 
323
  { // Change status to ON THE PHONE
 
324
    initialStatus = "PHN";
 
325
  }
 
326
  else
 
327
  { // Change status to ONLINE
 
328
    initialStatus = "NLN";
 
329
  }
 
330
  account->setInitialStatus( initialStatus );
 
331
 
 
332
  // Update the picture if we have one.
 
333
  if(hasTempImage_)
 
334
  {
 
335
    if( ! showImage)
 
336
    {
 
337
      // The temporary file is no longer needed.
 
338
      QFile::remove(tempImageFile_);
 
339
      hasTempImage_ = false;
 
340
    }
 
341
    else
 
342
    {
 
343
      imageName = account->getNewCustomImagePath();
 
344
 
 
345
      // Rename the temp file, place it over the new file
 
346
      imageFolder = QDir( QFileInfo(imageName).dir() );
 
347
      success = imageFolder.rename( tempImageFile_, imageName );
 
348
      hasTempImage_ = false;
 
349
 
 
350
      if( ! success )
 
351
      {
 
352
        kdWarning() << "The display picture file could not be renamed from \"" << tempImageFile_ << "\" to \"" << imageName << "\"." << endl;
 
353
      }
 
354
    }
 
355
  }
 
356
 
 
357
  // Create the MSN Object again.
 
358
  account->updateMsnObject();
 
359
}
 
360
 
 
361
 
 
362
 
 
363
/**
 
364
 * The user toggled the remember me option
 
365
 */
 
366
void AccountsWidget::rememberMeToggled(bool noGuest)
 
367
{
 
368
  autologinCheckBox_->setEnabled( noGuest );
 
369
  initialStatus_->setEnabled( noGuest );
 
370
}
 
371
 
 
372
 
 
373
 
 
374
/*
 
375
 * Allow the user to select an image, and convert it to 96x96, place it in a temp file.
 
376
 */
 
377
void AccountsWidget::selectImage( bool resize )
 
378
{
 
379
  // This code is partially borrowed from Kopete.
 
380
 
 
381
  KStandardDirs *dirs   = KGlobal::dirs();
 
382
  bool    isRemoteFile  = false;
 
383
  QString safeHandle    = 0;
 
384
  KURL    filePath      = 0;
 
385
  QString localFilePath = 0;
 
386
  QString tempImageName = 0;
 
387
  QImage  imageData     = 0;
 
388
 
 
389
  // all images:  dirs->findAllResources("data", "kdm/pics/users/*.png")
 
390
  // default dir: dirs->findResource("data", "kdm/pics/users/");
 
391
 
 
392
  // Get the resourcedir which contains the largest collection of images.
 
393
  // This fixes a problem with SuSE, where /etc/opt/kde/share is returned instead of /opt/kde3/share
 
394
  QString startDir;
 
395
  QStringList kdmDirs = dirs->findDirs("data", "kdm/pics/users/");
 
396
  QDir kdmDir;
 
397
  uint fileCount = 0;
 
398
  for( QStringList::Iterator it = kdmDirs.begin(); it != kdmDirs.end(); ++it )
 
399
  {
 
400
    kdmDir.setPath(*it);
 
401
    QStringList pngFiles = kdmDir.entryList("*.png");
 
402
    if( pngFiles.size() > fileCount )
 
403
    {
 
404
      fileCount = pngFiles.size();
 
405
      startDir = *it;
 
406
    }
 
407
  }
 
408
 
 
409
  // Show the open file dialog
 
410
  filePath = KFileDialog::getImageOpenURL( startDir, this, i18n( "Display Picture" ) );
 
411
  if(filePath.isEmpty())
 
412
  {
 
413
    return;
 
414
  }
 
415
 
 
416
  // Read the path.
 
417
  if(filePath.isLocalFile())
 
418
  {
 
419
    localFilePath = filePath.path();
 
420
  }
 
421
  else
 
422
  {
 
423
    // File is remote, download it to a local folder
 
424
    // first because QPixmap doesn't have KIO magic.
 
425
    // KIO::NetAccess::download fills the localFilePath field.
 
426
#if KDE_IS_VERSION(3,2,0)
 
427
    if(! KIO::NetAccess::download(filePath, localFilePath, this ))
 
428
#else
 
429
    if(! KIO::NetAccess::download(filePath, localFilePath ))
 
430
#endif
 
431
    {
 
432
      KMessageBox::sorry(this, i18n("Downloading of display image failed"), i18n("KMess"));
 
433
      return;
 
434
    }
 
435
 
 
436
    isRemoteFile = true;
 
437
  }
 
438
 
 
439
 
 
440
  // Convert the image to the correct format.
 
441
  // We use a temporary filename first and replace the original if you press OK.
 
442
  imageData = QImage(localFilePath);  // Load image data
 
443
 
 
444
#if KDE_IS_VERSION(3,4,0)
 
445
  // Allow the user to select a region of the image
 
446
  if( resize )
 
447
  {
 
448
    imageData = KPixmapRegionSelectorDialog::getSelectedImage( QPixmap(imageData), 96, 96, this );
 
449
  }
 
450
#else
 
451
  resize = false; // Avoid a compiler warning about unused variables.
 
452
#endif
 
453
 
 
454
  imageData = imageData.smoothScale(96, 96, QImage::ScaleMax); // Makes smallest edge 96 pixels, preserving aspect ratio
 
455
 
 
456
  if(! imageData.isNull() && (imageData.width() != 96 || imageData.height() != 96))
 
457
  {
 
458
    // Copy the middle part of the image.
 
459
    imageData = imageData.copy( (imageData.width() - 96) / 2,  // X
 
460
                                (imageData.height() - 96) / 2, // Y
 
461
                                96, 96 );
 
462
  }
 
463
 
 
464
 
 
465
  if(! imageData.isNull() && imageData.save(tempImageFile_, "PNG") )
 
466
  {
 
467
    // Sucess! Update the preview image
 
468
    customPixmap_ = QPixmap(tempImageFile_);
 
469
    pictureLabel_->setPixmap(customPixmap_);
 
470
    hasTempImage_ = true;
 
471
  }
 
472
  else
 
473
  {
 
474
    KMessageBox::sorry( this,
 
475
                        i18n( "An error occurred when trying to change the display picture.\n"
 
476
                              "Make sure that you have selected a correct image file" ),
 
477
                        i18n( "KMess" ) );
 
478
  }
 
479
 
 
480
  // Remove any temporary files
 
481
  if(isRemoteFile)
 
482
  {
 
483
    KIO::NetAccess::removeTempFile(localFilePath);
 
484
  }
 
485
}
 
486
 
 
487
 
 
488
 
 
489
// Slot running when the "Show image" checkbox was toggled.
 
490
void AccountsWidget::showImageToggled(bool noImage)
 
491
{
 
492
  browseButton_       -> setEnabled( ! noImage );
 
493
  pictureLabel_       -> setEnabled( ! noImage );
 
494
}
 
495
 
 
496
 
 
497
 
 
498
// The user pressed the "Create new account" button
 
499
void AccountsWidget::showRegisterPassport()
 
500
{
 
501
  new KRun("http://register.passport.com/");  // auto-deletes itself.
 
502
}
 
503
 
 
504
 
 
505
#include "accountswidget.moc"