kaddressbook Library API Documentation

actionmanager.cpp

00001 /*
00002   This file is part of KAddressBook.
00003   Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
00004 
00005   This program is free software; you can redistribute it and/or modify
00006   it under the terms of the GNU General Public License as published by
00007   the Free Software Foundation; either version 2 of the License, or
00008   (at your option) any later version.
00009 
00010   This program is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013   GNU General Public License for more details.
00014 
00015   You should have received a copy of the GNU General Public License
00016   along with this program; if not, write to the Free Software
00017   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019   As a special exception, permission is given to link this program
00020   with any edition of Qt, and distribute the resulting executable,
00021   without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qapplication.h>
00025 #include <qclipboard.h>
00026 
00027 #include <kapplication.h>
00028 #include <kconfig.h>
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 #include <kprotocolinfo.h>
00032 #include <kxmlguiclient.h>
00033 #include <kxmlguifactory.h>
00034 
00035 #include "actionmanager.h"
00036 #include "kaddressbook.h"
00037 #include "viewmanager.h"
00038 #include "undo.h"
00039 
00040 ActionManager::ActionManager(KXMLGUIClient *client, KAddressBook *widget,
00041                              bool readWrite, QObject *parent)
00042     : QObject(parent)
00043 {
00044     mGUIClient = client;
00045     mACollection = mGUIClient->actionCollection();
00046 
00047     mWidget = widget;
00048     connect( mWidget, SIGNAL( addresseeSelected( bool ) ),
00049              SLOT( addresseeSelected( bool ) ) );
00050     connect( mWidget, SIGNAL( modified( bool ) ),
00051              SLOT( modified( bool ) ) );
00052 
00053     mViewManager = mWidget->viewManager();
00054     connect( mViewManager, SIGNAL( viewConfigChanged(const QString &) ),
00055              SLOT( viewConfigChanged(const QString &) ) );
00056 
00057     connect( QApplication::clipboard(), SIGNAL( dataChanged() ),
00058              SLOT( clipboardDataChanged() ) );
00059 
00060     mReadWrite = readWrite;
00061     initReadOnlyActions();
00062     if (mReadWrite)
00063         initReadWriteActions();
00064 
00065     // Read our own config
00066     KConfig *config = kapp->config();
00067     config->setGroup("Views");
00068     mActiveViewName = config->readEntry("Active");
00069     config->setGroup("MainWindow");
00070     mActionJumpBar->setChecked(config->readBoolEntry("JumpBar", false));
00071     mActionFeatures->setCurrentItem(config->readNumEntry("Features", 0));
00072     mActionDetails->setChecked(config->readBoolEntry("Details", true));
00073     // Set the defaults
00074     addresseeSelected(false);
00075     modified(false);
00076     quickToolsAction();
00077 
00078     mActionViewList.setAutoDelete(true);
00079 
00080     // Connect to the signals from the undo/redo stacks so we can update the
00081     // edit menu
00082     connect(UndoStack::instance(), SIGNAL(changed()), SLOT(updateEditMenu()));
00083     connect(RedoStack::instance(), SIGNAL(changed()), SLOT(updateEditMenu()));
00084 }
00085 
00086 ActionManager::~ActionManager()
00087 {
00088     // Write our own config
00089     KConfig *config = kapp->config();
00090 
00091     config->setGroup("Views");
00092     config->writeEntry("Active", mActiveViewName);
00093 
00094     config->setGroup("MainWindow");
00095     config->writeEntry("JumpBar", mActionJumpBar->isChecked());
00096     config->writeEntry("Features", mActionFeatures->currentItem());
00097     config->writeEntry("Details", mActionDetails->isChecked());
00098 
00099     config->sync();
00100 }
00101 
00102 void ActionManager::setReadWrite(bool rw)
00103 {
00104     if (rw == mReadWrite)
00105         return;
00106 
00107     mReadWrite = rw;
00108     if (mReadWrite)
00109         initReadWriteActions();
00110     else
00111         destroyReadWriteActions();
00112 }
00113 
00114 
00115 void ActionManager::clipboardDataChanged()
00116 {
00117     if (mReadWrite)
00118         mActionPaste->setEnabled( !QApplication::clipboard()->text().isEmpty() );
00119 }
00120 
00121 void ActionManager::initReadOnlyActions()
00122 {
00123     // File menu
00124     mActionSave = new KAction(i18n("&Save"), "filesave", CTRL+Key_S, mWidget,
00125                               SLOT(save()), mACollection,"file_sync");
00126 
00127     new KAction(i18n("&New Contact..."), "filenew", CTRL+Key_N, mWidget,
00128                 SLOT(newAddressee()),mACollection,"file_new_contact");
00129 
00130     mActionMail = KStdAction::mail(mViewManager, SLOT(sendMail()),
00131                                    mACollection);
00132     mActionEditAddressee = new KAction(i18n("&Edit Contact..."), "edit", 0,
00133                                        mWidget, SLOT(editAddressee()),
00134                                        mACollection, "file_properties");
00135 
00136     KStdAction::print(mWidget, SLOT(print()), mACollection);
00137 
00138     new KAction(i18n("Import &KDE 2 Address Book..."), 0, mWidget,
00139                 SLOT(importKDE2()), mACollection, "file_import_kde2");
00140 
00141     new KAction(i18n("Import vCard..."), 0, mWidget, SLOT(importVCardSimple()),
00142                 mACollection, "file_import_vcard");
00143 
00144     new KAction(i18n("&Import List..."), 0, mWidget, SLOT(importCSV()),
00145                 mACollection, "file_import_csv");
00146 
00147     new KAction(i18n("&Export List..."), 0, mWidget, SLOT(exportCSV()),
00148                 mACollection, "file_export_csv");
00149 
00150     new KAction(i18n("&Export vCard 3.0..."), 0, mWidget, SLOT(exportVCard30()),
00151                 mACollection, "file_export_vcard30");
00152 
00153     // Edit menu
00154     mActionCopy = KStdAction::copy(mViewManager, SLOT(copy()),
00155                                    mACollection);
00156 
00157     KStdAction::selectAll(mViewManager, SLOT(setSelected()), mACollection);
00158 
00159     mActionDelete = new KAction(i18n("&Delete Contact"), "editdelete",
00160                                 Key_Delete, mViewManager,
00161                                 SLOT(deleteAddressee()), mACollection,
00162                                 "edit_delete");
00163 
00164     mActionUndo = KStdAction::undo(mWidget, SLOT(undo()), mACollection);
00165     mActionUndo->setEnabled(false);
00166 
00167     mActionRedo = KStdAction::redo(mWidget, SLOT(redo()), mACollection);
00168     mActionRedo->setEnabled( false );
00169 
00170     // View menu
00171     new KAction(i18n("Modify View..."), "configure", 0, mViewManager,
00172                 SLOT(modifyView()), mACollection,
00173                 "view_modify");
00174     new KAction(i18n("Add View..."), "window_new", 0, mViewManager,
00175                 SLOT(addView()), mACollection,
00176                 "view_add");
00177     mActionDeleteView = new KAction(i18n("Delete View"), "view_remove", 0,
00178                                     mViewManager,
00179                                     SLOT(deleteView()), mACollection,
00180                                     "view_delete");
00181     new KAction(i18n("Refresh View"), "reload", 0, mViewManager,
00182                 SLOT(refresh()), mACollection,
00183                 "view_refresh");
00184 
00185     // Only enable LDAP lookup if we can handle the protocol
00186     if( KProtocolInfo::isKnownProtocol( KURL("ldap://localhost") )) {
00187         // LDAP button on toolbar
00188         new KAction(i18n("&Lookup addresses in directory"),
00189                     "find", 0, mWidget, SLOT(slotOpenLDAPDialog()),
00190                     mACollection,"ldap_lookup");
00191     }
00192 
00193     // Settings menu
00194     mActionFeatures = new KSelectAction( i18n("Show Features Bar"),
00195                                          0, mACollection,
00196                                          "options_show_features" );
00197     connect( mActionFeatures, SIGNAL( activated( int ) ),
00198              mViewManager, SLOT( showFeatures( int ) ) );
00199     QStringList features;
00200     features << i18n("None") << i18n("Contact Editor")
00201              << i18n("Distribution Lists");
00202     mActionFeatures->setItems( features );
00203 
00204     mActionJumpBar = new KToggleAction(i18n("Show Jump Bar"), "next", 0,
00205                                        this, SLOT(quickToolsAction()),
00206                                        mACollection,
00207                                        "options_show_jump_bar");
00208     mActionDetails = new KToggleAction(i18n("Show Details"), 0,
00209                                        0, this, SLOT(quickToolsAction()),
00210                                        mACollection,
00211                                        "options_show_details");
00212     (void) new KAction(i18n("Edit &Filters..."), "filter",
00213                        0, mWidget, SLOT(configureFilters()),
00214                        mACollection, "options_edit_filters");
00215     mActionSelectFilter = new KSelectAction(i18n("Select Filter"), 0,
00216                                             mACollection, "select_filter");
00217 #if KDE_VERSION >= 309
00218     mActionSelectFilter->setMenuAccelsEnabled( false );
00219 #endif
00220 
00221     connect(mActionSelectFilter, SIGNAL(activated(int)),
00222             SLOT(slotFilterActivated(int)));
00223     connect(this, SIGNAL(filterActivated(int)),
00224             mViewManager, SLOT(filterActivated(int)));
00225     connect(mViewManager, SIGNAL(setFilterNames(const QStringList&)),
00226             SLOT(setFilterNames(const QStringList&)));
00227     connect(mViewManager, SIGNAL(setCurrentFilterName(const QString&)),
00228             SLOT(setCurrentFilterName(const QString&)));
00229     connect(mViewManager, SIGNAL(setCurrentFilter(int)),
00230             SLOT(setCurrentFilter(int)));
00231 }
00232 
00233 void ActionManager::initReadWriteActions()
00234 {
00235     // Edit menu
00236     mActionCut = KStdAction::cut(mViewManager, SLOT(cut()), mACollection);
00237     mActionPaste = KStdAction::paste(mViewManager, SLOT(paste()),
00238                                      mACollection);
00239     clipboardDataChanged();
00240 }
00241 
00242 void ActionManager::destroyReadWriteActions()
00243 {
00244     delete mActionCut;
00245     delete mActionPaste;
00246 }
00247 
00248 void ActionManager::updateEditMenu()
00249 {
00250     UndoStack *undo = UndoStack::instance();
00251     RedoStack *redo = RedoStack::instance();
00252 
00253     if (undo->isEmpty())
00254         mActionUndo->setText( i18n( "Undo" ) );
00255     else
00256         mActionUndo->setText( i18n( "Undo %1" ).arg(undo->top()->name()) );
00257     mActionUndo->setEnabled( !undo->isEmpty() );
00258 
00259     if (!redo->top())
00260         mActionRedo->setText( i18n( "Redo" ) );
00261     else
00262         mActionRedo->setText( i18n( "Redo %1" ).arg(redo->top()->name()) );
00263     mActionRedo->setEnabled( !redo->isEmpty() );
00264 }
00265 
00266 void ActionManager::addresseeSelected(bool selected)
00267 {
00268     if (mReadWrite)
00269     {
00270         mActionCut->setEnabled(selected);
00271     }
00272 
00273     mActionCopy->setEnabled(selected);
00274     mActionDelete->setEnabled(selected);
00275     mActionEditAddressee->setEnabled(selected);
00276     mActionMail->setEnabled(selected);
00277 }
00278 
00279 void ActionManager::modified(bool mod)
00280 {
00281     mModified = mod;
00282     mActionSave->setEnabled(mod);
00283 }
00284 
00285 void ActionManager::initActionViewList()
00286 {
00287     // Create the view actions, and set the active view
00288     // Find the last active view
00289     QStringList viewNameList = mViewManager->viewNames();
00290     KToggleAction *viewAction = 0;
00291 
00292     // Just incast there is no active view!
00293     if (mActiveViewName.isEmpty() ||
00294         (viewNameList.contains(mActiveViewName) == 0))
00295         mActiveViewName = *(viewNameList).at(0);
00296 
00297     // unplug the current ones
00298     mGUIClient->factory()->unplugActionList(mGUIClient, "view_loadedviews");
00299 
00300     // delete the current ones
00301     mActionViewList.clear();
00302     mActiveActionView = 0L;
00303 
00304     // Now find the active one, check the menu item, and raise it to the top
00305     QStringList::Iterator iter;
00306     QString viewName;
00307     for (iter = viewNameList.begin(); iter != viewNameList.end(); ++iter)
00308     {
00309         viewName = *iter;
00310 
00311         viewAction = new KToggleAction(viewName, QString::null, this,
00312                                        SLOT(selectViewAction()), mACollection,
00313                                        viewName.latin1());
00314 
00315         if (mActiveViewName == viewName)
00316         {
00317             mViewManager->setActiveView(viewName);
00318 
00319             viewAction->setChecked(true);
00320             mActiveActionView = viewAction;
00321         }
00322 
00323         mActionViewList.append(viewAction);
00324     }
00325 
00326     // Now append all the actions to the menu.
00327     mGUIClient->factory()->plugActionList(mGUIClient, "view_loadedviews",
00328                                           mActionViewList);
00329 }
00330 
00331 void ActionManager::viewConfigChanged(const QString &newActive)
00332 {
00333     if (!newActive.isEmpty())
00334     {
00335         mActiveViewName = newActive;
00336     }
00337 
00338     // we need to rebuild the actions
00339     initActionViewList();
00340 
00341     // Only enable delete if there is more than one view
00342     mActionDeleteView->setEnabled(mViewManager->viewNames().size() > 1);
00343 }
00344 
00345 void ActionManager::selectViewAction()
00346 {
00347     // See if we can find the selected action
00348     KToggleAction *action = 0;
00349 
00350     QString activatedName = sender()->name();
00351     QPtrListIterator<KAction> iter(mActionViewList);
00352     for (iter.toFirst(); iter.current(); ++iter)
00353     {
00354         action = dynamic_cast<KToggleAction*>(*iter);
00355 
00356         if (action->name() != activatedName)
00357             action->setChecked(false);
00358         else
00359         {
00360             mActiveActionView = action;
00361             mActiveActionView->setChecked(true);
00362             mActiveViewName = mActiveActionView->name();
00363 
00364             // Last, tell the main widget to set the view.
00365             mViewManager->setActiveView(mActiveViewName);
00366         }
00367     }
00368 }
00369 
00370 void ActionManager::quickToolsAction()
00371 {
00372     mViewManager->setJumpButtonBarVisible(mActionJumpBar->isChecked());
00373     mViewManager->showFeatures(mActionFeatures->currentItem());
00374     mViewManager->setDetailsVisible(mActionDetails->isChecked());
00375 }
00376 
00377 void ActionManager::setFilterNames(const QStringList& list)
00378 {
00379     QString current = mActionSelectFilter->currentText();
00380 
00381     QStringList items;
00382     items.append(i18n("None"));
00383     items+=list;
00384     mActionSelectFilter->setItems(items);
00385 
00386     setCurrentFilterName( current );
00387 }
00388 
00389 void ActionManager::slotFilterActivated(int index)
00390 {
00391     emit filterActivated(index-1);
00392 }
00393 
00394 void ActionManager::setCurrentFilterName(const QString& name)
00395 {
00396     QStringList items=mActionSelectFilter->items();
00397     int index=items.findIndex(name);
00398     if( index != -1 )
00399         setCurrentFilter(index);
00400 }
00401 
00402 void ActionManager::setCurrentFilter(int index)
00403 {
00404     mActionSelectFilter->setCurrentItem(index);
00405     emit filterActivated(index-1);
00406 }
00407 
00408 bool ActionManager::isModified()
00409 {
00410   return mModified;
00411 }
00412 
00413 #include "actionmanager.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:36 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001