1
/***************************************************************************
2
accountswidget.cpp - description
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
***************************************************************************/
10
/***************************************************************************
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. *
17
***************************************************************************/
19
#include "accountswidget.h"
22
#include <qcheckbox.h>
23
#include <qcombobox.h>
24
#include <qfileinfo.h>
29
#include <qlineedit.h>
32
#include <kmessagebox.h>
34
#include <kdeversion.h>
38
#include <kurllabel.h>
42
#include <kpopupmenu.h>
43
#include <kpushbutton.h>
44
#if KDE_IS_VERSION(3,4,0)
45
#include <kpixmapregionselectordialog.h>
49
#include <kio/netaccess.h>
52
#include <kstandarddirs.h>
53
#include <kfiledialog.h>
60
AccountsWidget::AccountsWidget( QWidget* parent, const char* name, WFlags fl )
61
: AccountsWidgetInterface( parent, name, fl ),
65
// Find the default image
66
KStandardDirs *dirs = KGlobal::dirs();
67
QString defaultImage = dirs->findResource( "data", "kmess/pics/kmesspic.png" );
69
// Load the image in the X-Server:
70
defaultPixmap_ = QPixmap(defaultImage);
71
customPixmap_ = defaultPixmap_;
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() ) );
81
// Plug the items into the browse menu button
82
browseSimple->plug( browsePopup );
83
browseResize->plug( browsePopup );
85
// Assign the new popup to the button and detach old connection
86
browseButton_->setPopup( browsePopup );
87
disconnect( browseButton_, SIGNAL( clicked() ) );
90
// Add the items to the initial status combo box
92
statuses << i18n("Online")
94
<< i18n("Be Right Back")
96
<< i18n("Out to Lunch")
97
<< i18n("On the Phone")
99
initialStatus_->insertStringList( statuses );
107
AccountsWidget::~AccountsWidget()
109
// Clean up the temp file.
112
QFile::remove(tempImageFile_);
115
// no need to delete child widgets, Qt does it all for us
120
// Return the filename of the currently selected image.
121
QString AccountsWidget::getPictureFileName() const
125
return tempImageFile_;
136
* Load the dialog settings.
138
void AccountsWidget::loadSettings(Account *account, bool isCurrentAccount)
143
myHandle_ = account->getHandle();
144
imageFile_ = account->getCustomImagePath();
145
tempImageFile_ = account->getCustomImagePath() + ".tmp";
146
showImage = account->getShowImage();
148
// Load default properties
149
friendlyNameEdit_ ->setText( account->getFriendlyName() );
150
handleEdit_ ->setText( myHandle_ );
151
passwordEdit_ ->setText( QString::null );
153
autologinCheckBox_ ->setChecked( account->getUseAutologin() );
156
// Make sure the drop down list matches the user's initial status
158
QString status = account->getInitialStatus();
159
if( status == "AWY" )
160
{ // Change status to AWAY
163
else if( status == "BRB" )
164
{ // Change status to BE RIGHT BACK
167
else if( status == "BSY" )
168
{ // Change status to BUSY
171
else if( status == "LUN" )
172
{ // Change status to OUT TO LUNCH
175
else if( status == "PHN" )
176
{ // Change status to ON THE PHONE
179
else if( status == "HDN" )
180
{ // Change status to INVISIBLE
184
{ // Change status to ONLINE
187
initialStatus_->setCurrentItem( item );
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 )
195
if( account->isVerified() )
197
friendlyNameEdit_ ->setEnabled( true );
198
registerLabel_ ->hide();
199
registerButton_ ->hide();
200
verifyLabel_ ->hide();
201
verifyButton_ ->hide();
205
friendlyNameEdit_ ->setEnabled( false );
206
registerLabel_ ->hide();
207
registerButton_ ->hide();
208
verifyLabel_ ->show();
209
verifyButton_ ->show();
214
friendlyNameEdit_ ->setEnabled( false );
215
registerLabel_ ->show();
216
registerButton_ ->show();
217
verifyLabel_ ->hide();
218
verifyButton_ ->hide();
221
// When the account is a guest account, offer a method to upgrade to a real account.
222
if( account->isGuestAccount() )
224
// Disable settings that require a real account which is saved in the config file.
225
rememberMeToggled(false);
229
// Hide the upgrade function
230
rememberCheckbox_->hide();
233
// Load the custom pixmap if we have one.
234
customPixmap_ = QPixmap( imageFile_ );
236
if(customPixmap_.isNull())
239
customPixmap_ = defaultPixmap_;
240
imageFile_ = QString::null;
244
pictureLabel_->setPixmap(customPixmap_);
246
// Set the "Show Image" checkbox
247
// Run the event manually this time to update the GUI.
248
noPictureCheckbox_->setChecked( ! showImage );
249
showImageToggled( ! showImage );
254
// Show the normal browse dialog to change display picture
255
void AccountsWidget::pictureBrowseSimple()
257
selectImage( false );
262
// Show the browse dialog to change display picture and then allow the user to crop it
263
void AccountsWidget::pictureBrowseResize()
271
* Save the settings in the account object.
273
void AccountsWidget::saveSettings( Account *account )
276
QString imageName = 0;
277
QString tempImageName = 0;
278
QString initialStatus;
284
showImage = ! noPictureCheckbox_->isChecked();
286
// When the account is a guest account, see if the user wanted to "upgrade" it.
287
if( account->isGuestAccount() )
289
if( rememberCheckbox_->isChecked() )
291
account->setGuestAccount(false);
295
// Update the settings
296
account->setLoginInformation( handleEdit_->text(), friendlyNameEdit_->text(), passwordEdit_->password() );
297
account->setUseAutologin( autologinCheckBox_->isChecked() );
298
account->setShowImage( showImage );
300
// Update the initial status
301
initialStatus = initialStatus_->currentText();
302
if( initialStatus == i18n("Away") )
303
{ // Change status to AWAY
304
initialStatus = "AWY";
306
else if( initialStatus == i18n("Be Right Back") )
307
{ // Change status to BE RIGHT BACK
308
initialStatus = "BRB";
310
else if( initialStatus == i18n("Busy") )
311
{ // Change status to BUSY
312
initialStatus = "BSY";
314
else if( initialStatus == i18n("Invisible") )
315
{ // Change status to INVISIBLE
316
initialStatus = "HDN";
318
else if( initialStatus == i18n("Out to Lunch") )
319
{ // Change status to OUT TO LUNCH
320
initialStatus = "LUN";
322
else if( initialStatus == i18n("On the Phone") )
323
{ // Change status to ON THE PHONE
324
initialStatus = "PHN";
327
{ // Change status to ONLINE
328
initialStatus = "NLN";
330
account->setInitialStatus( initialStatus );
332
// Update the picture if we have one.
337
// The temporary file is no longer needed.
338
QFile::remove(tempImageFile_);
339
hasTempImage_ = false;
343
imageName = account->getNewCustomImagePath();
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;
352
kdWarning() << "The display picture file could not be renamed from \"" << tempImageFile_ << "\" to \"" << imageName << "\"." << endl;
357
// Create the MSN Object again.
358
account->updateMsnObject();
364
* The user toggled the remember me option
366
void AccountsWidget::rememberMeToggled(bool noGuest)
368
autologinCheckBox_->setEnabled( noGuest );
369
initialStatus_->setEnabled( noGuest );
375
* Allow the user to select an image, and convert it to 96x96, place it in a temp file.
377
void AccountsWidget::selectImage( bool resize )
379
// This code is partially borrowed from Kopete.
381
KStandardDirs *dirs = KGlobal::dirs();
382
bool isRemoteFile = false;
383
QString safeHandle = 0;
385
QString localFilePath = 0;
386
QString tempImageName = 0;
387
QImage imageData = 0;
389
// all images: dirs->findAllResources("data", "kdm/pics/users/*.png")
390
// default dir: dirs->findResource("data", "kdm/pics/users/");
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
395
QStringList kdmDirs = dirs->findDirs("data", "kdm/pics/users/");
398
for( QStringList::Iterator it = kdmDirs.begin(); it != kdmDirs.end(); ++it )
401
QStringList pngFiles = kdmDir.entryList("*.png");
402
if( pngFiles.size() > fileCount )
404
fileCount = pngFiles.size();
409
// Show the open file dialog
410
filePath = KFileDialog::getImageOpenURL( startDir, this, i18n( "Display Picture" ) );
411
if(filePath.isEmpty())
417
if(filePath.isLocalFile())
419
localFilePath = filePath.path();
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 ))
429
if(! KIO::NetAccess::download(filePath, localFilePath ))
432
KMessageBox::sorry(this, i18n("Downloading of display image failed"), i18n("KMess"));
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
444
#if KDE_IS_VERSION(3,4,0)
445
// Allow the user to select a region of the image
448
imageData = KPixmapRegionSelectorDialog::getSelectedImage( QPixmap(imageData), 96, 96, this );
451
resize = false; // Avoid a compiler warning about unused variables.
454
imageData = imageData.smoothScale(96, 96, QImage::ScaleMax); // Makes smallest edge 96 pixels, preserving aspect ratio
456
if(! imageData.isNull() && (imageData.width() != 96 || imageData.height() != 96))
458
// Copy the middle part of the image.
459
imageData = imageData.copy( (imageData.width() - 96) / 2, // X
460
(imageData.height() - 96) / 2, // Y
465
if(! imageData.isNull() && imageData.save(tempImageFile_, "PNG") )
467
// Sucess! Update the preview image
468
customPixmap_ = QPixmap(tempImageFile_);
469
pictureLabel_->setPixmap(customPixmap_);
470
hasTempImage_ = true;
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" ),
480
// Remove any temporary files
483
KIO::NetAccess::removeTempFile(localFilePath);
489
// Slot running when the "Show image" checkbox was toggled.
490
void AccountsWidget::showImageToggled(bool noImage)
492
browseButton_ -> setEnabled( ! noImage );
493
pictureLabel_ -> setEnabled( ! noImage );
498
// The user pressed the "Create new account" button
499
void AccountsWidget::showRegisterPassport()
501
new KRun("http://register.passport.com/"); // auto-deletes itself.
505
#include "accountswidget.moc"