Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

addressbook.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 ** Copyright (C) 2003 Stefan Eilers (eilers.stefan@epost.de)
00004 **
00005 ** This file is part of the Open Palmtop Environment (see www.opie.info).
00006 **
00007 ** This file may be distributed and/or modified under the terms of the
00008 ** GNU General Public License version 2 as published by the Free Software
00009 ** Foundation and appearing in the file LICENSE.GPL included in the
00010 ** packaging of this file.
00011 **
00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 **
00015 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00016 **
00017 **
00018 **********************************************************************/
00019 
00020 #define QTOPIA_INTERNAL_FD
00021 
00022 // #include "addresssettings.h"
00023 #include "addressbook.h"
00024 
00025 #include <opie2/odebug.h>
00026 #include <opie2/ofileselector.h>
00027 #include <opie2/ofiledialog.h>
00028 #include <opie2/opimcontact.h>
00029 #include <opie2/ocontactaccessbackend_vcard.h>
00030 #include <opie2/oresource.h>
00031 
00032 #include <qpe/ir.h>
00033 #include <qpe/qpemessagebox.h>
00034 #include <qmenubar.h>
00035 #include <qpe/qpeapplication.h>
00036 
00037 #include <qaction.h>
00038 #include <qlayout.h>
00039 #include <qmessagebox.h>
00040 #include <qtoolbutton.h>
00041 
00042 #include <stdlib.h>
00043 #include <sys/stat.h>
00044 #include <sys/types.h>
00045 #include <fcntl.h>
00046 #include <unistd.h>
00047 
00048 
00049 #include "picker.h"
00050 #include "configdlg.h"
00051 
00052 extern QString addressbookPersonalVCardName();
00053 
00054 AddressbookWindow::AddressbookWindow( QWidget *parent, const char *name,
00055                                       WFlags /*f*/ )
00056         : Opie::OPimMainWindow( "Addressbook", "Contacts", tr( "Contact" ), "AddressBook",
00057                             parent, name, WType_TopLevel | WStyle_ContextHelp ),
00058           abEditor(0l),
00059           syncing(false)
00060 {
00061     setCaption( tr( "Contacts" ) );
00062 
00063         isLoading = true;
00064 
00065         m_config.load();
00066 
00067         // Create Views
00068         m_listContainer = new QWidget( this );
00069         QVBoxLayout *vb = new QVBoxLayout( m_listContainer );
00070 
00071         m_abView = new AbView( m_listContainer, m_config.orderList() );
00072         vb->addWidget( m_abView );
00073         connect( m_abView, SIGNAL(signalViewSwitched(int)),
00074                  this, SLOT(slotViewSwitched(int)) );
00075 
00076         QObject::connect( m_abView, SIGNAL(signalNotFound()), this, SLOT(slotNotFound()) );
00077 
00078         // Letter Picker
00079         pLabel = new LetterPicker( m_listContainer );
00080         connect(pLabel, SIGNAL(letterClicked(char)), this, SLOT(slotSetLetter(char)));
00081         connect(m_abView, SIGNAL(signalClearLetterPicker()), pLabel, SLOT(clear()) );
00082 
00083         vb->addWidget( pLabel );
00084 
00085     // Quick search bar
00086     m_searchBar = new OFloatBar( "Search", this, QMainWindow::Top, true );
00087     m_searchBar->setHorizontalStretchable( true );
00088     m_searchBar->hide();
00089     m_searchEdit = new QLineEdit( m_searchBar, "m_searchEdit" );
00090 
00091     m_searchBar->setStretchableWidget( m_searchEdit );
00092     connect( m_searchEdit, SIGNAL(returnPressed()), this, SLOT(slotFind()) );
00093 
00094     QAction *a = new QAction( tr( "Start Search" ),
00095                               Opie::Core::OResource::loadPixmap( "find", Opie::Core::OResource::SmallIcon ),
00096                               QString::null, 0, this, 0 );
00097     connect( a, SIGNAL(activated()), this, SLOT(slotFind()) );
00098     a->addTo( m_searchBar );
00099 
00100     // Insert Contact menu items
00101     QActionGroup *items = new QActionGroup( this, QString::null, false );
00102 
00103     m_actionMail = new QAction( tr( "Write Mail To" ),
00104                                 Opie::Core::OResource::loadPixmap( "addressbook/sendmail", Opie::Core::OResource::SmallIcon ),
00105                                 QString::null, 0, items, 0 );
00106     connect( m_actionMail, SIGNAL(activated()), this, SLOT(writeMail()) );
00107 
00108     a = new QAction( tr("Import vCard"),
00109                      Opie::Core::OResource::loadPixmap( "addressbook/fileimport", Opie::Core::OResource::SmallIcon ),
00110                      QString::null, 0, items, 0 );
00111     connect( a, SIGNAL(activated()), this, SLOT(importvCard()) );
00112 
00113     a = new QAction( tr("Export vCard"),
00114                      Opie::Core::OResource::loadPixmap( "addressbook/fileexport", Opie::Core::OResource::SmallIcon ),
00115                      QString::null, 0, items, 0 );
00116     connect( a, SIGNAL(activated()), this, SLOT(exportvCard()) );
00117 
00118     m_actionPersonal = new QAction( tr("My Personal Details"),
00119                                     Opie::Core::OResource::loadPixmap( "addressbook/identity", Opie::Core::OResource::SmallIcon ),
00120                                     QString::null, 0, items, 0 , true );
00121     connect( m_actionPersonal, SIGNAL(activated()), this, SLOT(slotPersonalView()) );
00122 
00123     insertItemMenuItems( items );
00124 
00125     // Insert View menu items
00126     items = new QActionGroup( this, QString::null, false );
00127 
00128     a = new QAction( tr("Show quick search bar"),QString::null, 0, items, 0, true );
00129     connect( a, SIGNAL(toggled(bool)), this, SLOT(slotShowFind(bool)) );
00130 
00131     insertViewMenuItems( items );
00132 
00133         // Fontsize
00134         defaultFont = new QFont( m_abView->font() );
00135         slotSetFont(m_config.fontSize());
00136         m_curFontSize = m_config.fontSize();
00137 
00138         setCentralWidget(m_listContainer);
00139 
00140         //      odebug << "adressbook contrsuction: t=" << t.elapsed() << oendl;
00141         connect( qApp, SIGNAL(flush()), this, SLOT(flush()) );
00142         connect( qApp, SIGNAL(reload()), this, SLOT(reload()) );
00143         connect( qApp, SIGNAL(appMessage(const QCString&,const QByteArray&)),
00144         this, SLOT(appMessage(const QCString&,const QByteArray&)) );
00145 
00146         isLoading = false;
00147 
00148     // Handle category selection
00149     setViewCategory( m_config.category() );
00150     m_abView->setShowByCategory( m_config.category() );
00151     connect( this, SIGNAL(categorySelected(const QString&)),
00152              this, SLOT(slotSetCategory(const QString&)) );
00153 }
00154 
00155 void AddressbookWindow::slotSetFont( int size )
00156 {
00157         odebug << "void AddressbookWindow::slotSetFont( " << size << " )" << oendl;
00158 
00159         if (size > 2 || size < 0)
00160                 size = 1;
00161 
00162         m_config.setFontSize( size );
00163 
00164         QFont *currentFont;
00165 
00166         switch (size) {
00167         case 0:
00168                 m_abView->setFont( QFont( defaultFont->family(), defaultFont->pointSize() - 2 ) );
00169                 currentFont = new QFont (m_abView->font());
00170                 // abList->resizeRows(currentFont->pixelSize() + 7); :SX
00171                 // abList->resizeRows();
00172                 break;
00173         case 1:
00174                 m_abView->setFont( *defaultFont );
00175                 currentFont = new QFont (m_abView->font());
00176 //              // abList->resizeRows(currentFont->pixelSize() + 7);
00177 //              abList->resizeRows();
00178                 break;
00179         case 2:
00180                 m_abView->setFont( QFont( defaultFont->family(), defaultFont->pointSize() + 2 ) );
00181                 currentFont = new QFont (m_abView->font());
00182 //              //abList->resizeRows(currentFont->pixelSize() + 7);
00183 //              abList->resizeRows();
00184                 break;
00185         }
00186 }
00187 
00188 
00189 
00190 void AddressbookWindow::importvCard() {
00191         QString str = Opie::Ui::OFileDialog::getOpenFileName( 1,"/");//,"", "*", this );
00192         if(!str.isEmpty() ){
00193                 setDocument((const QString&) str );
00194         }
00195 
00196 }
00197 void AddressbookWindow::exportvCard()
00198 {
00199         odebug << "void AddressbookWindow::exportvCard()" << oendl;
00200         QString filename = Opie::Ui::OFileDialog::getSaveFileName( 1,"/home/"); //,"", "*", this );
00201         if( !filename.isEmpty() &&  ( filename[filename.length()-1] != '/' ) ){
00202                 odebug << " Save to file " << filename << ", (" << filename.length()-1 << ")" << oendl;
00203                 Opie::OPimContact curCont = m_abView->currentEntry();
00204                 if ( !curCont.isEmpty() ){
00205                         Opie::OPimContactAccessBackend* vcard_backend = new Opie::OPimContactAccessBackend_VCard( QString::null,
00206                                                                                                 filename );
00207                         Opie::OPimContactAccess* access = new Opie::OPimContactAccess ( "addressbook_exp", QString::null , vcard_backend, true );
00208                         if ( access ){
00209                                 access->add( curCont );
00210                                 access->save();
00211                         }
00212                         delete access;
00213                 }else
00214                         QMessageBox::critical( 0, "Export VCard",
00215                            QString( tr( "You have to select a contact !") ) );
00216 
00217         }else
00218                 QMessageBox::critical( 0, "Export VCard",
00219                                        QString( tr( "You have to set a filename !") ) );
00220 }
00221 
00222 void AddressbookWindow::setDocument( const QString &filename )
00223 {
00224         odebug << "void AddressbookWindow::setDocument( " << filename << " )" << oendl;
00225 
00226         // Switch to default backend. This should avoid to import into
00227         // the personal database accidently.
00228         if ( m_actionPersonal->isOn() ){
00229                 m_actionPersonal->setOn( false );
00230                 slotPersonalView();
00231         }
00232 
00233         if ( filename.find(".vcf") != int(filename.length()) - 4 ){
00234 
00235 
00236 
00237                 switch( QMessageBox::information( this, tr ( "Right file type ?" ),
00238                                                   tr( "The selected file \n does not end with \".vcf\".\n Do you really want to open it?" ),
00239                                                   tr( "&Yes" ), tr( "&No" ), QString::null,
00240                                                   0,      // Enter == button 0
00241                                                   2 ) ) { // Escape == button 2
00242                 case 0:
00243                         odebug << "YES clicked" << oendl;
00244                         break;
00245                 case 1:
00246                         odebug << "NO clicked" << oendl;
00247                         return;
00248                         break;
00249                 }
00250         }
00251 
00252         Opie::OPimContactAccessBackend* vcard_backend = new Opie::OPimContactAccessBackend_VCard( QString::null,
00253                                                                          filename );
00254         Opie::OPimContactAccess* access = new Opie::OPimContactAccess ( "addressbook", QString::null , vcard_backend, true );
00255         Opie::OPimContactAccess::List allList = access->allRecords();
00256         odebug << "Found number of contacts in File: " << allList.count() << oendl;
00257 
00258         if ( !allList.count() ) {
00259                 QMessageBox::information( this, "Import VCard",
00260                                           "It was impossible to import\nthe VCard.\n"
00261                                           "The VCard may be corrupted!" );
00262         }
00263 
00264         bool doAsk = true;
00265         Opie::OPimContactAccess::List::Iterator it;
00266         for ( it = allList.begin(); it != allList.end(); ++it ){
00267                 odebug << "Adding Contact from: " << (*it).fullName() << oendl;
00268                 if ( doAsk ){
00269                         switch( QMessageBox::information( this, tr ( "Add Contact?" ),
00270                                                           tr( "Do you really want add contact for \n%1?" )
00271                                                           .arg( (*it).fullName().latin1() ),
00272                                                           tr( "&Yes" ), tr( "&No" ), tr( "&All Yes"),
00273                                                           0,      // Enter == button 0
00274                                                           2 ) ) { // Escape == button 2
00275                         case 0:
00276                                 odebug << "YES clicked" << oendl;
00277                                 m_abView->addEntry( *it );
00278                                 break;
00279                         case 1:
00280                                 odebug << "NO clicked" << oendl;
00281                                 break;
00282                         case 2:
00283                                 odebug << "YesAll clicked" << oendl;
00284                                 doAsk = false;
00285                                 break;
00286                         }
00287                 }else
00288                         m_abView->addEntry( *it );
00289 
00290         }
00291 
00292         delete access;
00293 }
00294 
00295 void AddressbookWindow::resizeEvent( QResizeEvent *e )
00296 {
00297         QMainWindow::resizeEvent( e );
00298 
00299 
00300 }
00301 
00302 AddressbookWindow::~AddressbookWindow()
00303 {
00304         ToolBarDock dock;
00305         int dummy;
00306         bool bDummy;
00307         getLocation ( listTools, dock, dummy, bDummy, dummy );
00308         m_config.setToolBarDock( dock );
00309         m_config.save();
00310 }
00311 
00312 int AddressbookWindow::create()
00313 {
00314     return 0;
00315 }
00316 
00317 bool AddressbookWindow::remove( int /*uid*/ )
00318 {
00319     return false;
00320 }
00321 
00322 void AddressbookWindow::beam( int /*uid*/ )
00323 {
00324 }
00325 
00326 void AddressbookWindow::show( int /*uid*/ )
00327 {
00328 }
00329 
00330 void AddressbookWindow::edit( int /*uid*/ )
00331 {
00332 }
00333 
00334 void AddressbookWindow::add( const Opie::OPimRecord& )
00335 {
00336 }
00337 
00338 void AddressbookWindow::slotItemNew()
00339 {
00340         Opie::OPimContact cnt;
00341         if( !syncing ) {
00342                 editEntry( NewEntry );
00343         } else {
00344                 QMessageBox::warning(this, tr("Contacts"),
00345                                      tr("Can not edit data, currently syncing"));
00346         }
00347 }
00348 
00349 void AddressbookWindow::slotItemEdit()
00350 {
00351         if(!syncing) {
00352                 if (m_actionPersonal->isOn()) {
00353                         editPersonal();
00354                 } else {
00355                         editEntry( EditEntry );
00356                 }
00357         } else {
00358                 QMessageBox::warning( this, tr("Contacts"),
00359                                       tr("Can not edit data, currently syncing") );
00360         }
00361 }
00362 
00363 void AddressbookWindow::slotItemDuplicate()
00364 {
00365     if(!syncing)
00366     {
00367         Opie::OPimContact entry = m_abView->currentEntry();
00368         entry.assignUid();
00369         m_abView->addEntry( entry );
00370         m_abView->setCurrentUid( entry.uid() );
00371     }
00372     else
00373     {
00374         QMessageBox::warning( this, tr("Contacts"),
00375                               tr("Can not edit data, currently syncing") );
00376     }
00377 }
00378 
00379 void AddressbookWindow::slotItemDelete()
00380 {
00381         if(!syncing) {
00382                 Opie::OPimContact tmpEntry = m_abView ->currentEntry();
00383 
00384                 // get a name, do the best we can...
00385                 QString strName = tmpEntry.fullName();
00386                 if ( strName.isEmpty() ) {
00387                         strName = tmpEntry.company();
00388                         if ( strName.isEmpty() )
00389                                 strName = "No Name";
00390                 }
00391 
00392 
00393                 if ( QPEMessageBox::confirmDelete( this, tr( "Contacts" ),
00394                                                    strName ) ) {
00395                         m_abView->removeEntry( tmpEntry.uid() );
00396                 }
00397         } else {
00398                 QMessageBox::warning( this, tr("Contacts"),
00399                                       tr("Can not edit data, currently syncing") );
00400         }
00401 }
00402 
00403 static const char * beamfile = "/tmp/obex/contact.vcf";
00404 
00405 void AddressbookWindow::slotItemBeam()
00406 {
00407         QString beamFilename;
00408         Opie::OPimContact c;
00409         if ( m_actionPersonal->isOn() ) {
00410                 beamFilename = addressbookPersonalVCardName();
00411                 if ( !QFile::exists( beamFilename ) )
00412                         return; // can't beam a non-existent file
00413                 Opie::OPimContactAccessBackend* vcard_backend = new Opie::OPimContactAccessBackend_VCard( QString::null,
00414                                                                                         beamFilename );
00415                 Opie::OPimContactAccess* access = new Opie::OPimContactAccess ( "addressbook", QString::null , vcard_backend, true );
00416                 Opie::OPimContactAccess::List allList = access->allRecords();
00417                 Opie::OPimContactAccess::List::Iterator it = allList.begin();  // Just take first
00418                 c = *it;
00419 
00420                 delete access;
00421         } else {
00422                 unlink( beamfile ); // delete if exists
00423                 mkdir("/tmp/obex/", 0755);
00424                 c = m_abView -> currentEntry();
00425                 Opie::OPimContactAccessBackend* vcard_backend = new Opie::OPimContactAccessBackend_VCard( QString::null,
00426                                                                                         beamfile );
00427                 Opie::OPimContactAccess* access = new Opie::OPimContactAccess ( "addressbook", QString::null , vcard_backend, true );
00428                 access->add( c );
00429                 access->save();
00430                 delete access;
00431 
00432                 beamFilename = beamfile;
00433         }
00434 
00435         odebug << "Beaming: " << beamFilename << oendl;
00436 
00437         Ir *ir = new Ir( this );
00438         connect( ir, SIGNAL( done(Ir*) ), this, SLOT( beamDone(Ir*) ) );
00439         QString description = c.fullName();
00440         ir->send( beamFilename, description, "text/x-vCard" );
00441 }
00442 
00443 void AddressbookWindow::slotItemFind()
00444 {
00445 }
00446 
00447 void AddressbookWindow::slotConfigure()
00448 {
00449         ConfigDlg* dlg = new ConfigDlg( this, "Config" );
00450         dlg -> setConfig( m_config );
00451         if ( QPEApplication::execDialog( dlg ) ) {
00452                 odebug << "Config Dialog accepted!" << oendl;
00453                 m_config = dlg -> getConfig();
00454                 if ( m_curFontSize != m_config.fontSize() ){
00455                         odebug << "Font was changed!" << oendl;
00456                         m_curFontSize = m_config.fontSize();
00457                         emit slotSetFont( m_curFontSize );
00458                 }
00459                 m_abView -> setListOrder( m_config.orderList() );
00460         }
00461 
00462         delete dlg;
00463 }
00464 
00465 void AddressbookWindow::slotShowFind( bool show )
00466 {
00467     if ( show )
00468     {
00469         // Display search bar
00470         m_searchBar->show();
00471         m_abView -> inSearch();
00472         m_searchEdit->setFocus();
00473     }
00474     else
00475     {
00476         // Hide search bar
00477         m_searchBar->hide();
00478         m_abView -> offSearch();
00479     }
00480 }
00481 
00482 void AddressbookWindow::slotFind()
00483 {
00484         m_abView->slotDoFind( m_searchEdit->text(), m_config.beCaseSensitive(), m_config.useRegExp(), false);
00485 
00486         m_searchEdit->clearFocus();
00487         // m_abView->setFocus();
00488 
00489 }
00490 
00491 void AddressbookWindow::slotViewBack()
00492 {
00493         // :SX showList();
00494 }
00495 
00496 void AddressbookWindow::writeMail()
00497 {
00498         Opie::OPimContact c = m_abView -> currentEntry();
00499         QString name = c.fileAs();
00500         QString email = c.defaultEmail();
00501 
00502         // I prefer the OPIE-Environment variable before the
00503         // QPE-one..
00504         QString basepath = QString::fromLatin1( getenv("OPIEDIR") );
00505         if ( basepath.isEmpty() )
00506                 basepath = QString::fromLatin1( getenv("QPEDIR") );
00507 
00508         // Try to access the preferred. If not possible, try to
00509         // switch to the other one..
00510         if ( m_config.useQtMail() ){
00511                 odebug << "Accessing: " << (basepath + "/bin/qtmail") << oendl;
00512                 if ( QFile::exists( basepath + "/bin/qtmail" ) ){
00513                         odebug << "QCop" << oendl;
00514                         QCopEnvelope e("QPE/Application/qtmail", "writeMail(QString,QString)");
00515                         e << name << email;
00516                         return;
00517                 } else
00518                         m_config.setUseOpieMail( true );
00519         }
00520         if ( m_config.useOpieMail() ){
00521                 odebug << "Accessing: " << (basepath + "/bin/opiemail") << oendl;
00522                 if ( QFile::exists( basepath + "/bin/opiemail" ) ){
00523                         odebug << "QCop" << oendl;
00524                         QCopEnvelope e("QPE/Application/opiemail", "writeMail(QString,QString)");
00525                         e << name << email;
00526                         return;
00527                 } else
00528                         m_config.setUseQtMail( true );
00529         }
00530 
00531 }
00532 
00533 void AddressbookWindow::beamDone( Ir *ir )
00534 {
00535 
00536         delete ir;
00537         unlink( beamfile );
00538 }
00539 
00540 
00541 static void parseName( const QString& name, QString *first, QString *middle,
00542                        QString * last )
00543 {
00544 
00545         int comma = name.find ( "," );
00546         QString rest;
00547         if ( comma > 0 ) {
00548                 *last = name.left( comma );
00549                 comma++;
00550                 while ( comma < int(name.length()) && name[comma] == ' ' )
00551                         comma++;
00552                 rest = name.mid( comma );
00553         } else {
00554                 int space = name.findRev( ' ' );
00555                 *last = name.mid( space+1 );
00556                 rest = name.left( space );
00557         }
00558         int space = rest.find( ' ' );
00559         if ( space <= 0 ) {
00560                 *first = rest;
00561         } else {
00562                 *first = rest.left( space );
00563                 *middle = rest.mid( space+1 );
00564         }
00565 
00566 }
00567 
00568 
00569 void AddressbookWindow::appMessage(const QCString &msg, const QByteArray &data)
00570 {
00571         bool needShow = false;
00572                 odebug << "Receiving QCop-Call with message " << msg << oendl;
00573 
00574 
00575         if (msg == "editPersonal()") {
00576                 editPersonal();
00577 
00578         // Categories might have changed, so reload
00579         reloadCategories();
00580         } else if (msg == "editPersonalAndClose()") {
00581                 editPersonal();
00582                 close();
00583         } else if ( msg == "addContact(QString,QString)" ) {
00584                 QDataStream stream(data,IO_ReadOnly);
00585                 QString name, email;
00586                 stream >> name >> email;
00587 
00588                 Opie::OPimContact cnt;
00589                 QString fn, mn, ln;
00590                 parseName( name, &fn, &mn, &ln );
00591                 //      odebug << " " << fn << " - " << mn " - " << ln << oendl;
00592                 cnt.setFirstName( fn );
00593                 cnt.setMiddleName( mn );
00594                 cnt.setLastName( ln );
00595                 cnt.insertEmails( email );
00596                 cnt.setDefaultEmail( email );
00597                 cnt.setFileAs();
00598 
00599                 m_abView -> addEntry( cnt );
00600 
00601                 // :SXm_abView()->init( cnt );
00602                 editEntry( EditEntry );
00603 
00604         // Categories might have changed, so reload
00605         reloadCategories();
00606         } else if ( msg == "beamBusinessCard()" ) {
00607                 QString beamFilename = addressbookPersonalVCardName();
00608                 if ( !QFile::exists( beamFilename ) )
00609                         return; // can't beam a non-existent file
00610 
00611                 Ir *ir = new Ir( this );
00612                 connect( ir, SIGNAL( done(Ir*) ), this, SLOT( beamDone(Ir*) ) );
00613                 QString description = "mycard.vcf";
00614                 ir->send( beamFilename, description, "text/x-vCard" );
00615         } else if ( msg == "show(int)" ) {
00616                 raise();
00617                 QDataStream stream(data,IO_ReadOnly);
00618                 int uid;
00619                 stream >> uid;
00620 
00621                 odebug << "Showing uid: " << uid << oendl;
00622 
00623                 // Deactivate Personal View..
00624                 if ( m_actionPersonal->isOn() ){
00625                         m_actionPersonal->setOn( false );
00626                         slotPersonalView();
00627                 }
00628 
00629                 // Reset category and show as card..
00630                 m_abView -> setShowByCategory( QString::null );
00631                 m_abView -> setCurrentUid( uid );
00632                 slotViewSwitched ( AbView::CardView );
00633 
00634                 needShow = true;
00635 
00636 
00637         } else if ( msg == "edit(int)" ) {
00638                 QDataStream stream(data,IO_ReadOnly);
00639                 int uid;
00640                 stream >> uid;
00641 
00642                 // Deactivate Personal View..
00643                 if ( m_actionPersonal->isOn() ){
00644                         m_actionPersonal->setOn( false );
00645                         slotPersonalView();
00646                 }
00647 
00648                 // Reset category and edit..
00649                 m_abView -> setShowByCategory( QString::null );
00650                 m_abView -> setCurrentUid( uid );
00651                 slotItemEdit();
00652 
00653         // Categories might have changed, so reload
00654         reloadCategories();
00655     }
00656 
00657         if (needShow)
00658             QPEApplication::setKeepRunning();
00659 }
00660 
00661 void AddressbookWindow::editEntry( EntryMode entryMode )
00662 {
00663         Opie::OPimContact entry;
00664         if ( !abEditor ) {
00665                 abEditor = new ContactEditor( entry, this, "editor" );
00666         }
00667         if ( entryMode == EditEntry )
00668                 abEditor->setEntry( m_abView -> currentEntry() );
00669         else if ( entryMode == NewEntry )
00670                 abEditor->setEntry( entry );
00671 
00672     // Set the dialog caption
00673     if ( m_actionPersonal->isOn() )
00674         abEditor->setCaption( tr( "Edit My Personal Details" ) );
00675     else
00676         abEditor->setCaption( tr( "Edit Contact" ) );
00677 
00678         // fix the focus...
00679         abEditor->setNameFocus();
00680         if ( QPEApplication::execDialog( abEditor ) == QDialog::Accepted ) {
00681                 setFocus();
00682                 if ( entryMode == NewEntry ) {
00683                         Opie::OPimContact insertEntry = abEditor->entry();
00684                         insertEntry.assignUid();
00685                         m_abView -> addEntry( insertEntry );
00686                         m_abView -> setCurrentUid( insertEntry.uid() );
00687                 } else {
00688                         Opie::OPimContact replEntry = abEditor->entry();
00689 
00690                         if ( !replEntry.isValidUid() )
00691                                 replEntry.assignUid();
00692 
00693                         m_abView -> replaceEntry( replEntry );
00694                 }
00695 
00696         // Categories might have changed, so reload
00697         reloadCategories();
00698         }
00699 }
00700 
00701 void AddressbookWindow::editPersonal()
00702 {
00703         Opie::OPimContact entry;
00704 
00705         // Switch to personal view if not selected
00706         // but take care of the menu, too
00707         if ( ! m_actionPersonal->isOn() ){
00708                 odebug << "*** ++++" << oendl;
00709                 m_actionPersonal->setOn( true );
00710                 slotPersonalView();
00711         }
00712 
00713         if ( !abEditor ) {
00714                 abEditor = new ContactEditor( entry, this, "editor" );
00715         }
00716 
00717         abEditor->setPersonalView( true );
00718         editEntry( EditEntry );
00719         abEditor->setPersonalView( false );
00720 
00721 }
00722 
00723 
00724 void AddressbookWindow::slotPersonalView()
00725 {
00726     odebug << "slotPersonalView()" << oendl;
00727 
00728     bool personal = m_actionPersonal->isOn();
00729 
00730     // Disable actions when showing personal details
00731     setItemNewEnabled( !personal );
00732     setItemDuplicateEnabled( !personal );
00733     setItemDeleteEnabled( !personal );
00734     m_actionMail->setEnabled( !personal );
00735     setShowCategories( !personal );
00736 
00737     // Display appropriate view
00738     m_abView->showPersonal( personal );
00739 
00740     if ( personal )
00741     {
00742         setCaption( tr( "Contacts - My Personal Details") );
00743 
00744         // Set category to 'All' to make sure personal details is visible
00745         setViewCategory( "All" );
00746         m_abView->setShowByCategory( "All" );
00747 
00748         // Temporarily disable letter picker
00749         pLabel->hide();
00750     }
00751     else
00752     {
00753         setCaption( tr( "Contacts") );
00754 
00755         // Re-enable letter picker
00756         pLabel->show();
00757     }
00758 }
00759 
00760 void AddressbookWindow::reload()
00761 {
00762         syncing = false;
00763         m_abView->clear();
00764         m_abView->reload();
00765 }
00766 
00767 void AddressbookWindow::flush()
00768 {
00769         syncing = true;
00770         m_abView->save();
00771 }
00772 
00773 
00774 void AddressbookWindow::closeEvent( QCloseEvent *e )
00775 {
00776     if ( active_view == AbView::CardView )
00777     {
00778         if ( !m_actionPersonal->isOn() )
00779         {
00780             // Switch to table view only if not editing personal details
00781             slotViewSwitched( AbView::TableView );
00782         }
00783         else
00784         {
00785             // If currently editing personal details, switch off personal view
00786             m_actionPersonal->setOn( false );
00787             slotPersonalView();
00788         }
00789 
00790         e->ignore();
00791         return;
00792         }
00793         if(syncing) {
00794                 /* shouldn't we save, I hear you say? well its already been set
00795                    so that an edit can not occur during a sync, and we flushed
00796                    at the start of the sync, so there is no need to save
00797                    Saving however itself would cause problems. */
00798                 e->accept();
00799                 return;
00800         }
00801         //################## shouldn't always save
00802         // True, but the database handles this automatically ! (se)
00803         if ( save() )
00804                 e->accept();
00805         else
00806                 e->ignore();
00807 }
00808 
00809 /*
00810   Returns true if it is OK to exit
00811 */
00812 
00813 bool AddressbookWindow::save()
00814 {
00815         if ( !m_abView->save() ) {
00816                 if ( QMessageBox::critical( 0, tr( "Out of space" ),
00817                                             tr("Unable to save information.\n"
00818                                                "Free up some space\n"
00819                                                "and try again.\n"
00820                                                "\nQuit anyway?"),
00821                                             QMessageBox::Yes|QMessageBox::Escape,
00822                                             QMessageBox::No|QMessageBox::Default )
00823                      != QMessageBox::No )
00824                         return true;
00825                 else
00826                         return false;
00827         }
00828         return true;
00829 }
00830 
00831 #ifdef __DEBUG_RELEASE
00832 void AddressbookWindow::slotSave()
00833 {
00834         save();
00835 }
00836 #endif
00837 
00838 
00839 void AddressbookWindow::slotNotFound()
00840 {
00841         odebug << "Got not found signal!" << oendl;
00842         QMessageBox::information( this, tr( "Not Found" ),
00843                                  "<qt>" + tr( "Unable to find a contact for this search pattern!" ) + "</qt>" );
00844 
00845 
00846 }
00847 void AddressbookWindow::slotWrapAround()
00848 {
00849         odebug << "Got wrap signal!" << oendl;
00850 //      if ( doNotifyWrapAround )
00851 //              QMessageBox::information( this, tr( "End of list" ),
00852 //                                        tr( "End of list. Wrap around now...!" ) + "\n" );
00853 
00854 }
00855 
00856 void AddressbookWindow::slotSetCategory( const QString &category )
00857 {
00858         odebug << "void AddressbookWindow::slotSetCategory( " << category << " )" << oendl;
00859 
00860         // Tell the view about the selected category
00861     QString cat = category;
00862     if ( cat == tr( "All" ) )
00863         cat = QString::null;
00864     m_config.setCategory( cat );
00865         m_abView -> setShowByCategory( cat );
00866 }
00867 
00868 void AddressbookWindow::slotViewSwitched( int view )
00869 {
00870         odebug << "void AddressbookWindow::slotViewSwitched( " << view << " )" << oendl;
00871 
00872         // Tell the view about the selected view
00873         m_abView -> setShowToView ( (AbView::Views) view );
00874         active_view = view;
00875 }
00876 
00877 
00878 void AddressbookWindow::slotListView()
00879 {
00880         slotViewSwitched( AbView::TableView );
00881 }
00882 
00883 void AddressbookWindow::slotCardView()
00884 {
00885         slotViewSwitched( AbView::CardView );
00886 }
00887 
00888 void AddressbookWindow::slotSetLetter( char c ) {
00889 
00890         m_abView->setShowByLetter( c, m_config.letterPickerSearch() );
00891 
00892 }
00893 

Generated on Sat Nov 5 16:15:46 2005 for OPIE by  doxygen 1.4.2