kalarm

mainwindow.cpp

00001 /*
00002  *  mainwindow.cpp  -  main application window
00003  *  Program:  kalarm
00004  *  Copyright © 2001-2006 by David Jarvie <software@astrojar.org.uk>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kalarm.h"
00022 
00023 #include <qiconset.h>
00024 #include <qdragobject.h>
00025 #include <qheader.h>
00026 
00027 #include <kmenubar.h>
00028 #include <ktoolbar.h>
00029 #include <kpopupmenu.h>
00030 #include <kaccel.h>
00031 #include <kaction.h>
00032 #include <kactionclasses.h>
00033 #include <kstdaction.h>
00034 #include <kiconloader.h>
00035 #include <kmessagebox.h>
00036 #include <kurldrag.h>
00037 #include <klocale.h>
00038 #include <kglobalsettings.h>
00039 #include <kconfig.h>
00040 #include <kkeydialog.h>
00041 #include <kedittoolbar.h>
00042 #include <kaboutdata.h>
00043 #include <dcopclient.h>
00044 #include <kdebug.h>
00045 
00046 #include <libkdepim/maillistdrag.h>
00047 #include <libkmime/kmime_content.h>
00048 #include <libkcal/calendarlocal.h>
00049 #include <libkcal/icaldrag.h>
00050 
00051 #include "alarmcalendar.h"
00052 #include "alarmevent.h"
00053 #include "alarmlistview.h"
00054 #include "alarmtext.h"
00055 #include "birthdaydlg.h"
00056 #include "daemon.h"
00057 #include "editdlg.h"
00058 #include "functions.h"
00059 #include "kalarmapp.h"
00060 #include "kamail.h"
00061 #include "prefdlg.h"
00062 #include "preferences.h"
00063 #include "synchtimer.h"
00064 #include "templatepickdlg.h"
00065 #include "templatedlg.h"
00066 #include "traywindow.h"
00067 #include "mainwindow.moc"
00068 
00069 using namespace KCal;
00070 
00071 static const char* UI_FILE     = "kalarmui.rc";
00072 static const char* WINDOW_NAME = "MainWindow";
00073 
00074 static QString   undoText;
00075 static QString   undoTextStripped;
00076 static QString   undoIcon;
00077 static KShortcut undoShortcut;
00078 static QString   redoText;
00079 static QString   redoTextStripped;
00080 static QString   redoIcon;
00081 static KShortcut redoShortcut;
00082 
00083 
00084 /*=============================================================================
00085 =  Class: MainWindow
00086 =============================================================================*/
00087 
00088 MainWindow::WindowList   MainWindow::mWindowList;
00089 TemplateDlg*             MainWindow::mTemplateDlg = 0;
00090 
00091 // Collect these widget labels together to ensure consistent wording and
00092 // translations across different modules.
00093 QString MainWindow::i18n_a_ShowAlarmTimes()    { return i18n("Show &Alarm Times"); }
00094 QString MainWindow::i18n_t_ShowAlarmTime()     { return i18n("Show alarm &time"); }
00095 QString MainWindow::i18n_m_ShowAlarmTime()     { return i18n("Show alarm ti&me"); }
00096 QString MainWindow::i18n_o_ShowTimeToAlarms()  { return i18n("Show Time t&o Alarms"); }
00097 QString MainWindow::i18n_n_ShowTimeToAlarm()   { return i18n("Show time u&ntil alarm"); }
00098 QString MainWindow::i18n_l_ShowTimeToAlarm()   { return i18n("Show time unti&l alarm"); }
00099 QString MainWindow::i18n_ShowExpiredAlarms()   { return i18n("Show Expired Alarms"); }
00100 QString MainWindow::i18n_e_ShowExpiredAlarms() { return i18n("Show &Expired Alarms"); }
00101 QString MainWindow::i18n_HideExpiredAlarms()   { return i18n("Hide Expired Alarms"); }
00102 QString MainWindow::i18n_e_HideExpiredAlarms() { return i18n("Hide &Expired Alarms"); }
00103 
00104 
00105 /******************************************************************************
00106 * Construct an instance.
00107 * To avoid resize() events occurring while still opening the calendar (and
00108 * resultant crashes), the calendar is opened before constructing the instance.
00109 */
00110 MainWindow* MainWindow::create(bool restored)
00111 {
00112     theApp()->checkCalendarDaemon();    // ensure calendar is open and daemon started
00113     return new MainWindow(restored);
00114 }
00115 
00116 MainWindow::MainWindow(bool restored)
00117     : MainWindowBase(0, 0, WGroupLeader | WStyle_ContextHelp | WDestructiveClose),
00118       mMinuteTimerActive(false),
00119       mHiddenTrayParent(false),
00120       mShowExpired(Preferences::showExpiredAlarms()),
00121       mShowTime(Preferences::showAlarmTime()),
00122       mShowTimeTo(Preferences::showTimeToAlarm())
00123 {
00124     kdDebug(5950) << "MainWindow::MainWindow()\n";
00125     setAutoSaveSettings(QString::fromLatin1(WINDOW_NAME));    // save window sizes etc.
00126     setPlainCaption(kapp->aboutData()->programName());
00127     if (!restored)
00128     {
00129         QSize s;
00130         if (KAlarm::readConfigWindowSize(WINDOW_NAME, s))
00131             resize(s);
00132     }
00133     KConfig* config = KGlobal::config();
00134     config->setGroup(QString::fromLatin1(WINDOW_NAME));
00135     QValueList<int> order = config->readIntListEntry(QString::fromLatin1("ColumnOrder"));
00136 
00137     setAcceptDrops(true);         // allow drag-and-drop onto this window
00138     if (!mShowTimeTo)
00139         mShowTime = true;     // ensure at least one time column is visible
00140     mListView = new AlarmListView(order, this, "listView");
00141     mListView->selectTimeColumns(mShowTime, mShowTimeTo);
00142     mListView->showExpired(mShowExpired);
00143     setCentralWidget(mListView);
00144     mListView->refresh();          // populate the alarm list
00145     mListView->clearSelection();
00146 
00147     connect(mListView, SIGNAL(itemDeleted()), SLOT(slotDeletion()));
00148     connect(mListView, SIGNAL(selectionChanged()), SLOT(slotSelection()));
00149     connect(mListView, SIGNAL(mouseButtonClicked(int, QListViewItem*, const QPoint&, int)),
00150             SLOT(slotMouseClicked(int, QListViewItem*, const QPoint&, int)));
00151     connect(mListView, SIGNAL(executed(QListViewItem*)), SLOT(slotDoubleClicked(QListViewItem*)));
00152     connect(mListView->header(), SIGNAL(indexChange(int, int, int)), SLOT(columnsReordered()));
00153     initActions();
00154 
00155     mWindowList.append(this);
00156     if (mWindowList.count() == 1  &&  Daemon::isDcopHandlerReady())
00157     {
00158         // It's the first main window, and the DCOP handler is ready
00159         if (theApp()->wantRunInSystemTray())
00160             theApp()->displayTrayIcon(true, this);     // create system tray icon for run-in-system-tray mode
00161         else if (theApp()->trayWindow())
00162             theApp()->trayWindow()->setAssocMainWindow(this);    // associate this window with the system tray icon
00163     }
00164     setUpdateTimer();
00165 }
00166 
00167 MainWindow::~MainWindow()
00168 {
00169     kdDebug(5950) << "MainWindow::~MainWindow()\n";
00170     mWindowList.remove(this);
00171     if (theApp()->trayWindow())
00172     {
00173         if (isTrayParent())
00174             delete theApp()->trayWindow();
00175         else
00176             theApp()->trayWindow()->removeWindow(this);
00177     }
00178     MinuteTimer::disconnect(this);
00179     mMinuteTimerActive = false;    // to ensure that setUpdateTimer() works correctly
00180     setUpdateTimer();
00181     MainWindow* main = mainMainWindow();
00182     if (main)
00183         KAlarm::writeConfigWindowSize(WINDOW_NAME, main->size());
00184     KToolBar* tb = toolBar();
00185     if (tb)
00186         tb->saveSettings(KGlobal::config(), "Toolbars");
00187     KGlobal::config()->sync();    // save any new window size to disc
00188     theApp()->quitIf();
00189 }
00190 
00191 /******************************************************************************
00192 * Save settings to the session managed config file, for restoration
00193 * when the program is restored.
00194 */
00195 void MainWindow::saveProperties(KConfig* config)
00196 {
00197     config->writeEntry(QString::fromLatin1("HiddenTrayParent"), isTrayParent() && isHidden());
00198     config->writeEntry(QString::fromLatin1("ShowExpired"), mShowExpired);
00199     config->writeEntry(QString::fromLatin1("ShowTime"), mShowTime);
00200     config->writeEntry(QString::fromLatin1("ShowTimeTo"), mShowTimeTo);
00201 }
00202 
00203 /******************************************************************************
00204 * Read settings from the session managed config file.
00205 * This function is automatically called whenever the app is being
00206 * restored. Read in whatever was saved in saveProperties().
00207 */
00208 void MainWindow::readProperties(KConfig* config)
00209 {
00210     mHiddenTrayParent = config->readBoolEntry(QString::fromLatin1("HiddenTrayParent"));
00211     mShowExpired      = config->readBoolEntry(QString::fromLatin1("ShowExpired"));
00212     mShowTime         = config->readBoolEntry(QString::fromLatin1("ShowTime"));
00213     mShowTimeTo       = config->readBoolEntry(QString::fromLatin1("ShowTimeTo"));
00214 }
00215 
00216 /******************************************************************************
00217 * Get the main main window, i.e. the parent of the system tray icon, or if
00218 * none, the first main window to be created. Visible windows take precedence
00219 * over hidden ones.
00220 */
00221 MainWindow* MainWindow::mainMainWindow()
00222 {
00223     MainWindow* tray = theApp()->trayWindow() ? theApp()->trayWindow()->assocMainWindow() : 0;
00224     if (tray  &&  tray->isVisible())
00225         return tray;
00226     for (WindowList::Iterator it = mWindowList.begin();  it != mWindowList.end();  ++it)
00227         if ((*it)->isVisible())
00228             return *it;
00229     if (tray)
00230         return tray;
00231     if (mWindowList.isEmpty())
00232         return 0;
00233     return mWindowList.first();
00234 }
00235 
00236 /******************************************************************************
00237 * Check whether this main window is the parent of the system tray icon.
00238 */
00239 bool MainWindow::isTrayParent() const
00240 {
00241     return theApp()->wantRunInSystemTray()  &&  theApp()->trayMainWindow() == this;
00242 }
00243 
00244 /******************************************************************************
00245 *  Close all main windows.
00246 */
00247 void MainWindow::closeAll()
00248 {
00249     while (mWindowList.first())
00250         delete mWindowList.first();    // N.B. the destructor removes the window from the list
00251 }
00252 
00253 /******************************************************************************
00254 *  Called when the window's size has changed (before it is painted).
00255 *  Sets the last column in the list view to extend at least to the right hand
00256 *  edge of the list view.
00257 *  Records the new size in the config file.
00258 */
00259 void MainWindow::resizeEvent(QResizeEvent* re)
00260 {
00261     // Save the window's new size only if it's the first main window
00262     if (mainMainWindow() == this)
00263         KAlarm::writeConfigWindowSize(WINDOW_NAME, re->size());
00264     MainWindowBase::resizeEvent(re);
00265 }
00266 
00267 /******************************************************************************
00268 *  Called when the window is first displayed.
00269 *  Sets the last column in the list view to extend at least to the right hand
00270 *  edge of the list view.
00271 */
00272 void MainWindow::showEvent(QShowEvent* se)
00273 {
00274     setUpdateTimer();
00275     slotUpdateTimeTo();
00276     MainWindowBase::showEvent(se);
00277 }
00278 
00279 /******************************************************************************
00280 *  Display the window.
00281 */
00282 void MainWindow::show()
00283 {
00284     MainWindowBase::show();
00285     if (mMenuError)
00286     {
00287         // Show error message now that the main window has been displayed.
00288         // Waiting until now lets the user easily associate the message with
00289         // the main window which is faulty.
00290         KMessageBox::error(this, i18n("Failure to create menus\n(perhaps %1 missing or corrupted)").arg(QString::fromLatin1(UI_FILE)));
00291         mMenuError = false;
00292     }
00293 }
00294 
00295 /******************************************************************************
00296 *  Called after the window is hidden.
00297 */
00298 void MainWindow::hideEvent(QHideEvent* he)
00299 {
00300     setUpdateTimer();
00301     MainWindowBase::hideEvent(he);
00302 }
00303 
00304 /******************************************************************************
00305 *  Called when the list's column order is changed.
00306 *  Save the new column order as the default the next time the program is run.
00307 */
00308 void MainWindow::columnsReordered()
00309 {
00310     KConfig* config = KGlobal::config();
00311     config->setGroup(QString::fromLatin1(WINDOW_NAME));
00312     config->writeEntry(QString::fromLatin1("ColumnOrder"), mListView->columnOrder());
00313     config->sync();
00314 }
00315 
00316 /******************************************************************************
00317 *  Initialise the menu, toolbar and main window actions.
00318 */
00319 void MainWindow::initActions()
00320 {
00321     KActionCollection* actions = actionCollection();
00322     mActionTemplates       = new KAction(i18n("&Templates..."), 0, this, SLOT(slotTemplates()), actions, "templates");
00323     mActionNew             = KAlarm::createNewAlarmAction(i18n("&New..."), this, SLOT(slotNew()), actions, "new");
00324     mActionNewFromTemplate = KAlarm::createNewFromTemplateAction(i18n("New &From Template"), this, SLOT(slotNewFromTemplate(const KAEvent&)), actions, "newFromTempl");
00325     mActionCreateTemplate  = new KAction(i18n("Create Tem&plate..."), 0, this, SLOT(slotNewTemplate()), actions, "createTemplate");
00326     mActionCopy            = new KAction(i18n("&Copy..."), "editcopy", Qt::SHIFT+Qt::Key_Insert, this, SLOT(slotCopy()), actions, "copy");
00327     mActionModify          = new KAction(i18n("&Edit..."), "edit", Qt::CTRL+Qt::Key_E, this, SLOT(slotModify()), actions, "modify");
00328     mActionDelete          = new KAction(i18n("&Delete"), "editdelete", Qt::Key_Delete, this, SLOT(slotDelete()), actions, "delete");
00329     mActionReactivate      = new KAction(i18n("Reac&tivate"), 0, Qt::CTRL+Qt::Key_R, this, SLOT(slotReactivate()), actions, "undelete");
00330     mActionEnable          = new KAction(QString::null, 0, Qt::CTRL+Qt::Key_B, this, SLOT(slotEnable()), actions, "disable");
00331     mActionView            = new KAction(i18n("&View"), "viewmag", Qt::CTRL+Qt::Key_W, this, SLOT(slotView()), actions, "view");
00332     mActionShowTime        = new KToggleAction(i18n_a_ShowAlarmTimes(), Qt::CTRL+Qt::Key_M, this, SLOT(slotShowTime()), actions, "showAlarmTimes");
00333     mActionShowTime->setCheckedState(i18n("Hide &Alarm Times"));
00334     mActionShowTimeTo      = new KToggleAction(i18n_o_ShowTimeToAlarms(), Qt::CTRL+Qt::Key_I, this, SLOT(slotShowTimeTo()), actions, "showTimeToAlarms");
00335     mActionShowTimeTo->setCheckedState(i18n("Hide Time t&o Alarms"));
00336     mActionShowExpired     = new KToggleAction(i18n_e_ShowExpiredAlarms(), "history", Qt::CTRL+Qt::Key_P, this, SLOT(slotShowExpired()), actions, "showExpiredAlarms");
00337     mActionShowExpired->setCheckedState(i18n_e_HideExpiredAlarms());
00338     mActionToggleTrayIcon  = new KToggleAction(i18n("Show in System &Tray"), 0, this, SLOT(slotToggleTrayIcon()), actions, "showInSystemTray");
00339     mActionToggleTrayIcon->setCheckedState(i18n("Hide From System &Tray"));
00340     new KAction(i18n("Import &Alarms..."), 0, this, SLOT(slotImportAlarms()), actions, "importAlarms");
00341     new KAction(i18n("Import &Birthdays..."), 0, this, SLOT(slotBirthdays()), actions, "importBirthdays");
00342     new KAction(i18n("&Refresh Alarms"), "reload", 0, this, SLOT(slotResetDaemon()), actions, "refreshAlarms");
00343     Daemon::createAlarmEnableAction(actions, "alarmEnable");
00344     if (undoText.isNull())
00345     {
00346         // Get standard texts, etc., for Undo and Redo actions
00347         KAction* act = KStdAction::undo(this, 0, actions);
00348         undoIcon         = act->icon();
00349         undoShortcut     = act->shortcut();
00350         undoText         = act->text();
00351         undoTextStripped = KAlarm::stripAccel(undoText);
00352         delete act;
00353         act = KStdAction::redo(this, 0, actions);
00354         redoIcon         = act->icon();
00355         redoShortcut     = act->shortcut();
00356         redoText         = act->text();
00357         redoTextStripped = KAlarm::stripAccel(redoText);
00358         delete act;
00359     }
00360     mActionUndo = new KToolBarPopupAction(undoText, undoIcon, undoShortcut, this, SLOT(slotUndo()), actions, "edit_undo");
00361     mActionRedo = new KToolBarPopupAction(redoText, redoIcon, redoShortcut, this, SLOT(slotRedo()), actions, "edit_redo");
00362     KStdAction::find(mListView, SLOT(slotFind()), actions);
00363     mActionFindNext = KStdAction::findNext(mListView, SLOT(slotFindNext()), actions);
00364     mActionFindPrev = KStdAction::findPrev(mListView, SLOT(slotFindPrev()), actions);
00365     KStdAction::selectAll(mListView, SLOT(slotSelectAll()), actions);
00366     KStdAction::deselect(mListView, SLOT(slotDeselect()), actions);
00367     KStdAction::quit(this, SLOT(slotQuit()), actions);
00368     KStdAction::keyBindings(this, SLOT(slotConfigureKeys()), actions);
00369     KStdAction::configureToolbars(this, SLOT(slotConfigureToolbar()), actions);
00370     KStdAction::preferences(this, SLOT(slotPreferences()), actions);
00371     setStandardToolBarMenuEnabled(true);
00372     createGUI(UI_FILE);
00373 
00374     mContextMenu = static_cast<KPopupMenu*>(factory()->container("listContext", this));
00375     mActionsMenu = static_cast<KPopupMenu*>(factory()->container("actions", this));
00376     mMenuError = (!mContextMenu  ||  !mActionsMenu);
00377     connect(mActionsMenu, SIGNAL(aboutToShow()), SLOT(updateActionsMenu()));
00378     connect(mActionUndo->popupMenu(), SIGNAL(aboutToShow()), SLOT(slotInitUndoMenu()));
00379     connect(mActionUndo->popupMenu(), SIGNAL(activated(int)), SLOT(slotUndoItem(int)));
00380     connect(mActionRedo->popupMenu(), SIGNAL(aboutToShow()), SLOT(slotInitRedoMenu()));
00381     connect(mActionRedo->popupMenu(), SIGNAL(activated(int)), SLOT(slotRedoItem(int)));
00382     connect(Undo::instance(), SIGNAL(changed(const QString&, const QString&)), SLOT(slotUndoStatus(const QString&, const QString&)));
00383     connect(mListView, SIGNAL(findActive(bool)), SLOT(slotFindActive(bool)));
00384     Preferences::connect(SIGNAL(preferencesChanged()), this, SLOT(updateTrayIconAction()));
00385     connect(theApp(), SIGNAL(trayIconToggled()), SLOT(updateTrayIconAction()));
00386 
00387     // Set menu item states
00388     setEnableText(true);
00389     mActionShowTime->setChecked(mShowTime);
00390     mActionShowTimeTo->setChecked(mShowTimeTo);
00391     mActionShowExpired->setChecked(mShowExpired);
00392     if (!Preferences::expiredKeepDays())
00393         mActionShowExpired->setEnabled(false);
00394     updateTrayIconAction();         // set the correct text for this action
00395     mActionUndo->setEnabled(Undo::haveUndo());
00396     mActionRedo->setEnabled(Undo::haveRedo());
00397     mActionFindNext->setEnabled(false);
00398     mActionFindPrev->setEnabled(false);
00399 
00400     mActionCopy->setEnabled(false);
00401     mActionModify->setEnabled(false);
00402     mActionDelete->setEnabled(false);
00403     mActionReactivate->setEnabled(false);
00404     mActionView->setEnabled(false);
00405     mActionEnable->setEnabled(false);
00406     mActionCreateTemplate->setEnabled(false);
00407 
00408     KToolBar* tb = toolBar();
00409     if (tb)
00410         tb->applySettings(KGlobal::config(), "Toolbars");
00411 
00412     Undo::emitChanged();     // set the Undo/Redo menu texts
00413     Daemon::checkStatus();
00414     Daemon::monitoringAlarms();
00415 }
00416 
00417 /******************************************************************************
00418 * Enable or disable the Templates menu item in every main window instance.
00419 */
00420 void MainWindow::enableTemplateMenuItem(bool enable)
00421 {
00422     for (WindowList::Iterator it = mWindowList.begin();  it != mWindowList.end();  ++it)
00423         (*it)->mActionTemplates->setEnabled(enable);
00424 }
00425 
00426 /******************************************************************************
00427 * Refresh the alarm list in every main window instance.
00428 */
00429 void MainWindow::refresh()
00430 {
00431     kdDebug(5950) << "MainWindow::refresh()\n";
00432     for (WindowList::Iterator it = mWindowList.begin();  it != mWindowList.end();  ++it)
00433         (*it)->mListView->refresh();
00434 }
00435 
00436 /******************************************************************************
00437 * Refresh the alarm list in every main window instance which is displaying
00438 * expired alarms.
00439 * Called when an expired alarm setting changes in the user preferences.
00440 */
00441 void MainWindow::updateExpired()
00442 {
00443     kdDebug(5950) << "MainWindow::updateExpired()\n";
00444     bool enableShowExpired = Preferences::expiredKeepDays();
00445     for (WindowList::Iterator it = mWindowList.begin();  it != mWindowList.end();  ++it)
00446     {
00447         MainWindow* w = *it;
00448         if (w->mShowExpired)
00449         {
00450             if (!enableShowExpired)
00451                 w->slotShowExpired();
00452             else
00453                 w->mListView->refresh();
00454         }
00455         w->mActionShowExpired->setEnabled(enableShowExpired);
00456     }
00457 }
00458 
00459 /******************************************************************************
00460 * Called when the show-alarm-time or show-time-to-alarm setting changes in the
00461 * user preferences.
00462 * Update the alarm list in every main window instance to show the new default
00463 * columns. No change is made if a window isn't using the old settings.
00464 */
00465 void MainWindow::updateTimeColumns(bool oldTime, bool oldTimeTo)
00466 {
00467     kdDebug(5950) << "MainWindow::updateShowAlarmTimes()\n";
00468     bool newTime   = Preferences::showAlarmTime();
00469     bool newTimeTo = Preferences::showTimeToAlarm();
00470     if (!newTime  &&  !newTimeTo)
00471         newTime = true;     // at least one time column must be displayed
00472     if (!oldTime  &&  !oldTimeTo)
00473         oldTime = true;     // at least one time column must have been displayed
00474     if (newTime != oldTime  ||  newTimeTo != oldTimeTo)
00475     {
00476         for (WindowList::Iterator it = mWindowList.begin();  it != mWindowList.end();  ++it)
00477         {
00478             MainWindow* w = *it;
00479             if (w->mShowTime   == oldTime
00480             &&  w->mShowTimeTo == oldTimeTo)
00481             {
00482                 w->mShowTime   = newTime;
00483                 w->mShowTimeTo = newTimeTo;
00484                 w->mActionShowTime->setChecked(newTime);
00485                 w->mActionShowTimeTo->setChecked(newTimeTo);
00486                 w->mListView->selectTimeColumns(newTime, newTimeTo);
00487             }
00488         }
00489         setUpdateTimer();
00490     }
00491 }
00492 
00493 /******************************************************************************
00494 * Start or stop the timer which updates the time-to-alarm values every minute.
00495 * Should be called whenever a main window is created or destroyed, or shown or
00496 * hidden.
00497 */
00498 void MainWindow::setUpdateTimer()
00499 {
00500     // Check whether any windows need to be updated
00501     MainWindow* needTimer = 0;
00502     MainWindow* timerWindow = 0;
00503     for (WindowList::Iterator it = mWindowList.begin();  it != mWindowList.end();  ++it)
00504     {
00505         MainWindow* w = *it;
00506         if (w->isVisible()  &&  w->mListView->showingTimeTo())
00507             needTimer = w;
00508         if (w->mMinuteTimerActive)
00509             timerWindow = w;
00510     }
00511 
00512     // Start or stop the update timer if necessary
00513     if (needTimer  &&  !timerWindow)
00514     {
00515         // Timeout every minute.
00516         needTimer->mMinuteTimerActive = true;
00517         MinuteTimer::connect(needTimer, SLOT(slotUpdateTimeTo()));
00518         kdDebug(5950) << "MainWindow::setUpdateTimer(): started timer" << endl;
00519     }
00520     else if (!needTimer  &&  timerWindow)
00521     {
00522         timerWindow->mMinuteTimerActive = false;
00523         MinuteTimer::disconnect(timerWindow);
00524         kdDebug(5950) << "MainWindow::setUpdateTimer(): stopped timer" << endl;
00525     }
00526 }
00527 /******************************************************************************
00528 * Update the time-to-alarm values for each main window which is displaying them.
00529 */
00530 void MainWindow::slotUpdateTimeTo()
00531 {
00532     kdDebug(5950) << "MainWindow::slotUpdateTimeTo()" << endl;
00533     for (WindowList::Iterator it = mWindowList.begin();  it != mWindowList.end();  ++it)
00534     {
00535         MainWindow* w = *it;
00536         if (w->isVisible()  &&  w->mListView->showingTimeTo())
00537             w->mListView->updateTimeToAlarms();
00538     }
00539 }
00540 
00541 /******************************************************************************
00542 * Select an alarm in the displayed list.
00543 */
00544 void MainWindow::selectEvent(const QString& eventID)
00545 {
00546     mListView->clearSelection();
00547     AlarmListViewItem* item = mListView->getEntry(eventID);
00548     if (item)
00549     {
00550         mListView->setSelected(item, true);
00551         mListView->setCurrentItem(item);
00552         mListView->ensureItemVisible(item);
00553     }
00554 }
00555 
00556 /******************************************************************************
00557 *  Called when the New button is clicked to edit a new alarm to add to the list.
00558 */
00559 void MainWindow::slotNew()
00560 {
00561     executeNew(this);
00562 }
00563 
00564 /******************************************************************************
00565 *  Execute a New Alarm dialog, optionally either presetting it to the supplied
00566 *  event, or setting the action and text.
00567 */
00568 void MainWindow::executeNew(MainWindow* win, const KAEvent* evnt, KAEvent::Action action, const AlarmText& text)
00569 {
00570     EditAlarmDlg editDlg(false, i18n("New Alarm"), win, "editDlg", evnt);
00571     if (!text.isEmpty())
00572         editDlg.setAction(action, text);
00573     if (editDlg.exec() == QDialog::Accepted)
00574     {
00575         KAEvent event;
00576         editDlg.getEvent(event);
00577 
00578         // Add the alarm to the displayed lists and to the calendar file
00579         if (KAlarm::addEvent(event, (win ? win->mListView : 0)) == KAlarm::UPDATE_KORG_ERR)
00580             KAlarm::displayKOrgUpdateError(win, KAlarm::KORG_ERR_ADD, 1);
00581         Undo::saveAdd(event);
00582 
00583         KAlarm::outputAlarmWarnings(&editDlg, &event);
00584     }
00585 }
00586 
00587 /******************************************************************************
00588 *  Called when a template is selected from the New From Template popup menu.
00589 *  Executes a New Alarm dialog, preset from the selected template.
00590 */
00591 void MainWindow::slotNewFromTemplate(const KAEvent& tmplate)
00592 {
00593     executeNew(this, &tmplate);
00594 }
00595 
00596 /******************************************************************************
00597 *  Called when the New Template button is clicked to create a new template
00598 *  based on the currently selected alarm.
00599 */
00600 void MainWindow::slotNewTemplate()
00601 {
00602     AlarmListViewItem* item = mListView->selectedItem();
00603     if (item)
00604     {
00605         KAEvent event = item->event();
00606         TemplateDlg::createTemplate(&event, this);
00607     }
00608 }
00609 
00610 /******************************************************************************
00611 *  Called when the Copy button is clicked to edit a copy of an existing alarm,
00612 *  to add to the list.
00613 */
00614 void MainWindow::slotCopy()
00615 {
00616     AlarmListViewItem* item = mListView->selectedItem();
00617     if (item)
00618         executeNew(this, &item->event());
00619 }
00620 
00621 /******************************************************************************
00622 *  Called when the Modify button is clicked to edit the currently highlighted
00623 *  alarm in the list.
00624 */
00625 void MainWindow::slotModify()
00626 {
00627     AlarmListViewItem* item = mListView->selectedItem();
00628     if (item)
00629     {
00630         KAEvent event = item->event();
00631         executeEdit(event, this);
00632     }
00633 }
00634 
00635 /******************************************************************************
00636 *  Open the Edit Alarm dialogue to edit the specified alarm.
00637 */
00638 void MainWindow::executeEdit(KAEvent& event, MainWindow* win)
00639 {
00640     EditAlarmDlg editDlg(false, i18n("Edit Alarm"), win, "editDlg", &event);
00641     if (editDlg.exec() == QDialog::Accepted)
00642     {
00643         KAEvent newEvent;
00644         bool changeDeferral = !editDlg.getEvent(newEvent);
00645 
00646         // Update the event in the displays and in the calendar file
00647         AlarmListView* view = win ? win->mListView : 0;
00648         if (changeDeferral)
00649         {
00650             // The only change has been to an existing deferral
00651             KAlarm::updateEvent(newEvent, view, true, false);   // keep the same event ID
00652         }
00653         else
00654         {
00655             if (KAlarm::modifyEvent(event, newEvent, view) == KAlarm::UPDATE_KORG_ERR)
00656                 KAlarm::displayKOrgUpdateError(win, KAlarm::KORG_ERR_MODIFY, 1);
00657         }
00658         Undo::saveEdit(event, newEvent);
00659 
00660         KAlarm::outputAlarmWarnings(&editDlg, &newEvent);
00661     }
00662 }
00663 
00664 /******************************************************************************
00665 *  Called when the View button is clicked to view the currently highlighted
00666 *  alarm in the list.
00667 */
00668 void MainWindow::slotView()
00669 {
00670     AlarmListViewItem* item = mListView->selectedItem();
00671     if (item)
00672     {
00673         KAEvent event = item->event();
00674         EditAlarmDlg editDlg(false, (event.expired() ? i18n("Expired Alarm") + " [" + i18n("read-only") + "]"
00675                                                      : i18n("View Alarm")),
00676                              this, "editDlg", &event, true);
00677         editDlg.exec();
00678     }
00679 }
00680 
00681 /******************************************************************************
00682 *  Called when the Delete button is clicked to delete the currently highlighted
00683 *  alarms in the list.
00684 */
00685 void MainWindow::slotDelete()
00686 {
00687     QValueList<EventListViewItemBase*> items = mListView->selectedItems();
00688     if (Preferences::confirmAlarmDeletion())
00689     {
00690         int n = items.count();
00691         if (KMessageBox::warningContinueCancel(this, i18n("Do you really want to delete the selected alarm?",
00692                                                           "Do you really want to delete the %n selected alarms?", n),
00693                                                i18n("Delete Alarm", "Delete Alarms", n),
00694                                                KGuiItem(i18n("&Delete"), "editdelete"),
00695                                                Preferences::CONFIRM_ALARM_DELETION)
00696             != KMessageBox::Continue)
00697             return;
00698     }
00699 
00700     int warnKOrg = 0;
00701     QValueList<KAEvent> events;
00702     AlarmCalendar::activeCalendar()->startUpdate();    // prevent multiple saves of the calendars until we're finished
00703     AlarmCalendar::expiredCalendar()->startUpdate();
00704     for (QValueList<EventListViewItemBase*>::Iterator it = items.begin();  it != items.end();  ++it)
00705     {
00706         AlarmListViewItem* item = (AlarmListViewItem*)(*it);
00707         KAEvent event = item->event();
00708 
00709         // Delete the event from the calendar and displays
00710         events.append(event);
00711         if (KAlarm::deleteEvent(event) == KAlarm::UPDATE_KORG_ERR)
00712             ++warnKOrg;
00713     }
00714     AlarmCalendar::activeCalendar()->endUpdate();      // save the calendars now
00715     AlarmCalendar::expiredCalendar()->endUpdate();
00716     Undo::saveDeletes(events);
00717 
00718     if (warnKOrg)
00719         KAlarm::displayKOrgUpdateError(this, KAlarm::KORG_ERR_DELETE, warnKOrg);
00720 }
00721 
00722 /******************************************************************************
00723 *  Called when the Reactivate button is clicked to reinstate the currently
00724 *  highlighted expired alarms in the list.
00725 */
00726 void MainWindow::slotReactivate()
00727 {
00728     int warnKOrg = 0;
00729     QValueList<KAEvent> events;
00730     QValueList<EventListViewItemBase*> items = mListView->selectedItems();
00731     mListView->clearSelection();
00732     AlarmCalendar::activeCalendar()->startUpdate();    // prevent multiple saves of the calendars until we're finished
00733     AlarmCalendar::expiredCalendar()->startUpdate();
00734     for (QValueList<EventListViewItemBase*>::Iterator it = items.begin();  it != items.end();  ++it)
00735     {
00736         // Add the alarm to the displayed lists and to the calendar file
00737         AlarmListViewItem* item = (AlarmListViewItem*)(*it);
00738         KAEvent event = item->event();
00739         events.append(event);
00740         if (KAlarm::reactivateEvent(event, mListView, true) == KAlarm::UPDATE_KORG_ERR)
00741             ++warnKOrg;
00742     }
00743     AlarmCalendar::activeCalendar()->endUpdate();      // save the calendars now
00744     AlarmCalendar::expiredCalendar()->endUpdate();
00745     Undo::saveReactivates(events);
00746 
00747     if (warnKOrg)
00748         KAlarm::displayKOrgUpdateError(this, KAlarm::KORG_ERR_ADD, warnKOrg);
00749 }
00750 
00751 /******************************************************************************
00752 *  Called when the Enable/Disable button is clicked to enable or disable the
00753 *  currently highlighted alarms in the list.
00754 */
00755 void MainWindow::slotEnable()
00756 {
00757     bool enable = mActionEnableEnable;    // save since changed in response to KAlarm::enableEvent()
00758     QValueList<EventListViewItemBase*> items = mListView->selectedItems();
00759     AlarmCalendar::activeCalendar()->startUpdate();    // prevent multiple saves of the calendars until we're finished
00760     for (QValueList<EventListViewItemBase*>::Iterator it = items.begin();  it != items.end();  ++it)
00761     {
00762         AlarmListViewItem* item = (AlarmListViewItem*)(*it);
00763         KAEvent event = item->event();
00764 
00765         // Enable the alarm in the displayed lists and in the calendar file
00766         KAlarm::enableEvent(event, mListView, enable);
00767     }
00768     AlarmCalendar::activeCalendar()->endUpdate();      // save the calendars now
00769 }
00770 
00771 /******************************************************************************
00772 *  Called when the Show Alarm Times menu item is selected or deselected.
00773 */
00774 void MainWindow::slotShowTime()
00775 {
00776     mShowTime = !mShowTime;
00777     mActionShowTime->setChecked(mShowTime);
00778     if (!mShowTime  &&  !mShowTimeTo)
00779         slotShowTimeTo();    // at least one time column must be displayed
00780     else
00781         mListView->selectTimeColumns(mShowTime, mShowTimeTo);
00782 }
00783 
00784 /******************************************************************************
00785 *  Called when the Show Time To Alarms menu item is selected or deselected.
00786 */
00787 void MainWindow::slotShowTimeTo()
00788 {
00789     mShowTimeTo = !mShowTimeTo;
00790     mActionShowTimeTo->setChecked(mShowTimeTo);
00791     if (!mShowTimeTo  &&  !mShowTime)
00792         slotShowTime();    // at least one time column must be displayed
00793     else
00794         mListView->selectTimeColumns(mShowTime, mShowTimeTo);
00795     setUpdateTimer();
00796 }
00797 
00798 /******************************************************************************
00799 *  Called when the Show Expired Alarms menu item is selected or deselected.
00800 */
00801 void MainWindow::slotShowExpired()
00802 {
00803     mShowExpired = !mShowExpired;
00804     mActionShowExpired->setChecked(mShowExpired);
00805     mActionShowExpired->setToolTip(mShowExpired ? i18n_HideExpiredAlarms() : i18n_ShowExpiredAlarms());
00806     mListView->showExpired(mShowExpired);
00807     mListView->refresh();
00808 }
00809 
00810 /******************************************************************************
00811 *  Called when the Import Alarms menu item is selected, to merge alarms from an
00812 *  external calendar into the current calendars.
00813 */
00814 void MainWindow::slotImportAlarms()
00815 {
00816     if (AlarmCalendar::importAlarms(this))
00817         mListView->refresh();
00818 }
00819 
00820 /******************************************************************************
00821 *  Called when the Import Birthdays menu item is selected, to display birthdays
00822 *  from the address book for selection as alarms.
00823 */
00824 void MainWindow::slotBirthdays()
00825 {
00826     BirthdayDlg dlg(this);
00827     if (dlg.exec() == QDialog::Accepted)
00828     {
00829         QValueList<KAEvent> events = dlg.events();
00830         if (events.count())
00831         {
00832             mListView->clearSelection();
00833             int warnKOrg = 0;
00834             for (QValueList<KAEvent>::Iterator ev = events.begin();  ev != events.end();  ++ev)
00835             {
00836                 // Add alarm to the displayed lists and to the calendar file
00837                 if (KAlarm::addEvent(*ev, mListView) == KAlarm::UPDATE_KORG_ERR)
00838                     ++warnKOrg;
00839             }
00840             if (warnKOrg)
00841                 KAlarm::displayKOrgUpdateError(this, KAlarm::KORG_ERR_ADD, warnKOrg);
00842             KAlarm::outputAlarmWarnings(&dlg);
00843         }
00844     }
00845 }
00846 
00847 /******************************************************************************
00848 *  Called when the Templates menu item is selected, to display the alarm
00849 *  template editing dialogue.
00850 */
00851 void MainWindow::slotTemplates()
00852 {
00853     if (!mTemplateDlg)
00854     {
00855         mTemplateDlg = TemplateDlg::create(this);
00856         enableTemplateMenuItem(false);     // disable menu item in all windows
00857         connect(mTemplateDlg, SIGNAL(finished()), SLOT(slotTemplatesEnd()));
00858         mTemplateDlg->show();
00859     }
00860 }
00861 
00862 /******************************************************************************
00863 *  Called when the alarm template editing dialogue has exited.
00864 */
00865 void MainWindow::slotTemplatesEnd()
00866 {
00867     if (mTemplateDlg)
00868     {
00869         mTemplateDlg->delayedDestruct();   // this deletes the dialogue once it is safe to do so
00870         mTemplateDlg = 0;
00871         enableTemplateMenuItem(true);      // re-enable menu item in all windows
00872     }
00873 }
00874 
00875 /******************************************************************************
00876 *  Called when the Display System Tray Icon menu item is selected.
00877 */
00878 void MainWindow::slotToggleTrayIcon()
00879 {
00880     theApp()->displayTrayIcon(!theApp()->trayIconDisplayed(), this);
00881 }
00882 
00883 /******************************************************************************
00884 * Called when the system tray icon is created or destroyed.
00885 * Set the system tray icon menu text according to whether or not the system
00886 * tray icon is currently visible.
00887 */
00888 void MainWindow::updateTrayIconAction()
00889 {
00890     mActionToggleTrayIcon->setEnabled(theApp()->haveSystemTray() && !theApp()->wantRunInSystemTray());
00891     mActionToggleTrayIcon->setChecked(theApp()->trayIconDisplayed());
00892 }
00893 
00894 /******************************************************************************
00895 * Called when the Actions menu is about to be displayed.
00896 * Update the status of the Alarms Enabled menu item.
00897 */
00898 void MainWindow::updateActionsMenu()
00899 {
00900     Daemon::checkStatus();   // update the Alarms Enabled item status
00901 }
00902 
00903 /******************************************************************************
00904 *  Called when the active status of Find changes.
00905 */
00906 void MainWindow::slotFindActive(bool active)
00907 {
00908     mActionFindNext->setEnabled(active);
00909     mActionFindPrev->setEnabled(active);
00910 }
00911 
00912 /******************************************************************************
00913 *  Called when the Undo action is selected.
00914 */
00915 void MainWindow::slotUndo()
00916 {
00917     Undo::undo(this, KAlarm::stripAccel(mActionUndo->text()));
00918 }
00919 
00920 /******************************************************************************
00921 *  Called when the Redo action is selected.
00922 */
00923 void MainWindow::slotRedo()
00924 {
00925     Undo::redo(this, KAlarm::stripAccel(mActionRedo->text()));
00926 }
00927 
00928 /******************************************************************************
00929 *  Called when an Undo item is selected.
00930 */
00931 void MainWindow::slotUndoItem(int id)
00932 {
00933     Undo::undo(id, this, Undo::actionText(Undo::UNDO, id));
00934 }
00935 
00936 /******************************************************************************
00937 *  Called when a Redo item is selected.
00938 */
00939 void MainWindow::slotRedoItem(int id)
00940 {
00941     Undo::redo(id, this, Undo::actionText(Undo::REDO, id));
00942 }
00943 
00944 /******************************************************************************
00945 *  Called when the Undo menu is about to show.
00946 *  Populates the menu.
00947 */
00948 void MainWindow::slotInitUndoMenu()
00949 {
00950     initUndoMenu(mActionUndo->popupMenu(), Undo::UNDO);
00951 }
00952 
00953 /******************************************************************************
00954 *  Called when the Redo menu is about to show.
00955 *  Populates the menu.
00956 */
00957 void MainWindow::slotInitRedoMenu()
00958 {
00959     initUndoMenu(mActionRedo->popupMenu(), Undo::REDO);
00960 }
00961 
00962 /******************************************************************************
00963 *  Populate the undo or redo menu.
00964 */
00965 void MainWindow::initUndoMenu(KPopupMenu* menu, Undo::Type type)
00966 {
00967     menu->clear();
00968     const QString& action = (type == Undo::UNDO) ? undoTextStripped : redoTextStripped;
00969     QValueList<int> ids = Undo::ids(type);
00970     for (QValueList<int>::ConstIterator it = ids.begin();  it != ids.end();  ++it)
00971     {
00972         int id = *it;
00973         menu->insertItem(i18n("Undo [action]: message", "%1 %2: %3")
00974                                .arg(action).arg(Undo::actionText(type, id)).arg(Undo::description(type, id)), id);
00975     }
00976 }
00977 
00978 /******************************************************************************
00979 *  Called when the status of the Undo or Redo list changes.
00980 *  Change the Undo or Redo text to include the action which would be undone/redone.
00981 */
00982 void MainWindow::slotUndoStatus(const QString& undo, const QString& redo)
00983 {
00984     if (undo.isNull())
00985     {
00986         mActionUndo->setEnabled(false);
00987         mActionUndo->setText(undoText);
00988     }
00989     else
00990     {
00991         mActionUndo->setEnabled(true);
00992         mActionUndo->setText(QString("%1 %2").arg(undoText).arg(undo));
00993     }
00994     if (redo.isNull())
00995     {
00996         mActionRedo->setEnabled(false);
00997         mActionRedo->setText(redoText);
00998     }
00999     else
01000     {
01001         mActionRedo->setEnabled(true);
01002         mActionRedo->setText(QString("%1 %2").arg(redoText).arg(redo));
01003     }
01004 }
01005 
01006 /******************************************************************************
01007 *  Called when the Reset Daemon menu item is selected.
01008 */
01009 void MainWindow::slotResetDaemon()
01010 {
01011     KAlarm::resetDaemon();
01012 }
01013 
01014 /******************************************************************************
01015 *  Called when the "Configure KAlarm" menu item is selected.
01016 */
01017 void MainWindow::slotPreferences()
01018 {
01019     KAlarmPrefDlg prefDlg;
01020     prefDlg.exec();
01021 }
01022 
01023 /******************************************************************************
01024 *  Called when the Configure Keys menu item is selected.
01025 */
01026 void MainWindow::slotConfigureKeys()
01027 {
01028     KKeyDialog::configure(actionCollection(), this);
01029 }
01030 
01031 /******************************************************************************
01032 *  Called when the Configure Toolbars menu item is selected.
01033 */
01034 void MainWindow::slotConfigureToolbar()
01035 {
01036     saveMainWindowSettings(KGlobal::config(), WINDOW_NAME);
01037     KEditToolbar dlg(factory());
01038     connect(&dlg, SIGNAL(newToolbarConfig()), this, SLOT(slotNewToolbarConfig()));
01039     dlg.exec();
01040 }
01041 
01042 /******************************************************************************
01043 *  Called when OK or Apply is clicked in the Configure Toolbars dialog, to save
01044 *  the new configuration.
01045 */
01046 void MainWindow::slotNewToolbarConfig()
01047 {
01048     createGUI(UI_FILE);
01049     applyMainWindowSettings(KGlobal::config(), WINDOW_NAME);
01050 }
01051 
01052 /******************************************************************************
01053 *  Called when the Quit menu item is selected.
01054 */
01055 void MainWindow::slotQuit()
01056 {
01057     theApp()->doQuit(this);
01058 }
01059 
01060 /******************************************************************************
01061 *  Called when the user or the session manager attempts to close the window.
01062 */
01063 void MainWindow::closeEvent(QCloseEvent* ce)
01064 {
01065     if (!theApp()->sessionClosingDown()  &&  isTrayParent())
01066     {
01067         // The user (not the session manager) wants to close the window.
01068         // It's the parent window of the system tray icon, so just hide
01069         // it to prevent the system tray icon closing.
01070         hide();
01071         theApp()->quitIf();
01072         ce->ignore();
01073     }
01074     else
01075         ce->accept();
01076 }
01077 
01078 /******************************************************************************
01079 *  Called when an item is deleted from the ListView.
01080 *  Disables the actions if no item is still selected.
01081 */
01082 void MainWindow::slotDeletion()
01083 {
01084     if (!mListView->selectedCount())
01085     {
01086         kdDebug(5950) << "MainWindow::slotDeletion(true)\n";
01087         mActionCreateTemplate->setEnabled(false);
01088         mActionCopy->setEnabled(false);
01089         mActionModify->setEnabled(false);
01090         mActionView->setEnabled(false);
01091         mActionDelete->setEnabled(false);
01092         mActionReactivate->setEnabled(false);
01093         mActionEnable->setEnabled(false);
01094     }
01095 }
01096 
01097 /******************************************************************************
01098 *  Called when the drag cursor enters the window.
01099 */
01100 void MainWindow::dragEnterEvent(QDragEnterEvent* e)
01101 {
01102     executeDragEnterEvent(e);
01103 }
01104 
01105 /******************************************************************************
01106 *  Called when the drag cursor enters a main or system tray window, to accept
01107 *  or reject the dragged object.
01108 */
01109 void MainWindow::executeDragEnterEvent(QDragEnterEvent* e)
01110 {
01111     if (KCal::ICalDrag::canDecode(e))
01112         e->accept(!AlarmListView::dragging());   // don't accept "text/calendar" objects from KAlarm
01113     else
01114         e->accept(QTextDrag::canDecode(e)
01115                || KURLDrag::canDecode(e)
01116                || KPIM::MailListDrag::canDecode(e));
01117 }
01118 
01119 /******************************************************************************
01120 *  Called when an object is dropped on the window.
01121 *  If the object is recognised, the edit alarm dialog is opened appropriately.
01122 */
01123 void MainWindow::dropEvent(QDropEvent* e)
01124 {
01125     executeDropEvent(this, e);
01126 }
01127 
01128 static QString getMailHeader(const char* header, KMime::Content& content)
01129 {
01130     KMime::Headers::Base* hd = content.getHeaderByType(header);
01131     return hd ? hd->asUnicodeString() : QString::null;
01132 }
01133 
01134 /******************************************************************************
01135 *  Called when an object is dropped on a main or system tray window, to
01136 *  evaluate the action required and extract the text.
01137 */
01138 void MainWindow::executeDropEvent(MainWindow* win, QDropEvent* e)
01139 {
01140     KAEvent::Action action = KAEvent::MESSAGE;
01141     QString        text;
01142     QByteArray     bytes;
01143     AlarmText      alarmText;
01144     KPIM::MailList mailList;
01145     KURL::List     files;
01146     KCal::CalendarLocal calendar(QString::fromLatin1("UTC"));
01147     calendar.setLocalTime();    // default to local time (i.e. no time zone)
01148 #ifndef NDEBUG
01149     QCString fmts;
01150     for (int idbg = 0;  e->format(idbg);  ++idbg)
01151     {
01152         if (idbg) fmts += ", ";
01153         fmts += e->format(idbg);
01154     }
01155     kdDebug(5950) << "MainWindow::executeDropEvent(): " << fmts << endl;
01156 #endif
01157 
01158     /* The order of the tests below matters, since some dropped objects
01159      * provide more than one mime type.
01160      * Don't change them without careful thought !!
01161      */
01162     if (e->provides("message/rfc822")
01163     &&  !(bytes = e->encodedData("message/rfc822")).isEmpty())
01164     {
01165         // Email message(s). Ignore all but the first.
01166         kdDebug(5950) << "MainWindow::executeDropEvent(email)" << endl;
01167         QCString mails(bytes.data(), bytes.size());
01168         KMime::Content content;
01169         content.setContent(mails);
01170         content.parse();
01171         QString body;
01172         if (content.textContent())
01173             content.textContent()->decodedText(body, true, true);    // strip trailing newlines & spaces
01174         unsigned long sernum = 0;
01175         if (e->provides(KPIM::MailListDrag::format())
01176         &&  KPIM::MailListDrag::decode(e, mailList)
01177         &&  mailList.count())
01178         {
01179             // Get its KMail serial number to allow the KMail message
01180             // to be called up from the alarm message window.
01181             sernum = mailList.first().serialNumber();
01182         }
01183         alarmText.setEmail(getMailHeader("To", content),
01184                            getMailHeader("From", content),
01185                            getMailHeader("Cc", content),
01186                            getMailHeader("Date", content),
01187                            getMailHeader("Subject", content),
01188                    body, sernum);
01189     }
01190     else if (KURLDrag::decode(e, files)  &&  files.count())
01191     {
01192         kdDebug(5950) << "MainWindow::executeDropEvent(URL)" << endl;
01193         action = KAEvent::FILE;
01194         alarmText.setText(files.first().prettyURL());
01195     }
01196     else if (e->provides(KPIM::MailListDrag::format())
01197     &&       KPIM::MailListDrag::decode(e, mailList))
01198     {
01199         // KMail message(s). Ignore all but the first.
01200         kdDebug(5950) << "MainWindow::executeDropEvent(KMail_list)" << endl;
01201         if (!mailList.count())
01202             return;
01203         KPIM::MailSummary& summary = mailList.first();
01204         QDateTime dt;
01205         dt.setTime_t(summary.date());
01206         QString body = KAMail::getMailBody(summary.serialNumber());
01207         alarmText.setEmail(summary.to(), summary.from(), QString::null,
01208                            KGlobal::locale()->formatDateTime(dt), summary.subject(),
01209                            body, summary.serialNumber());
01210     }
01211     else if (KCal::ICalDrag::decode(e, &calendar))
01212     {
01213         // iCalendar - ignore all but the first event
01214         kdDebug(5950) << "MainWindow::executeDropEvent(iCalendar)" << endl;
01215         KCal::Event::List events = calendar.rawEvents();
01216         if (!events.isEmpty())
01217         {
01218             KAEvent ev(*events.first());
01219             executeNew(win, &ev);
01220         }
01221         return;
01222     }
01223     else if (QTextDrag::decode(e, text))
01224     {
01225         kdDebug(5950) << "MainWindow::executeDropEvent(text)" << endl;
01226         alarmText.setText(text);
01227     }
01228     else
01229         return;
01230 
01231     if (!alarmText.isEmpty())
01232         executeNew(win, 0, action, alarmText);
01233 }
01234 
01235 /******************************************************************************
01236 *  Called when the selected items in the ListView changes.
01237 *  Selects the new current item, and enables the actions appropriately.
01238 */
01239 void MainWindow::slotSelection()
01240 {
01241     // Find which item has been selected, and whether more than one is selected
01242     QValueList<EventListViewItemBase*> items = mListView->selectedItems();
01243     int count = items.count();
01244     AlarmListViewItem* item = (AlarmListViewItem*)((count == 1) ? items.first() : 0);
01245     bool enableReactivate = true;
01246     bool enableEnableDisable = true;
01247     bool enableEnable = false;
01248     bool enableDisable = false;
01249     QDateTime now = QDateTime::currentDateTime();
01250     for (QValueList<EventListViewItemBase*>::Iterator it = items.begin();  it != items.end();  ++it)
01251     {
01252         const KAEvent& event = ((AlarmListViewItem*)(*it))->event();
01253         if (enableReactivate
01254         &&  (!event.expired()  ||  !event.occursAfter(now, true)))
01255             enableReactivate = false;
01256         if (enableEnableDisable)
01257         {
01258             if (event.expired())
01259                 enableEnableDisable = enableEnable = enableDisable = false;
01260             else
01261             {
01262                 if (!enableEnable  &&  !event.enabled())
01263                     enableEnable = true;
01264                 if (!enableDisable  &&  event.enabled())
01265                     enableDisable = true;
01266             }
01267         }
01268     }
01269 
01270     kdDebug(5950) << "MainWindow::slotSelection(true)\n";
01271     mActionCreateTemplate->setEnabled(count == 1);
01272     mActionCopy->setEnabled(count == 1);
01273     mActionModify->setEnabled(item && !mListView->expired(item));
01274     mActionView->setEnabled(count == 1);
01275     mActionDelete->setEnabled(count);
01276     mActionReactivate->setEnabled(count && enableReactivate);
01277     mActionEnable->setEnabled(enableEnable || enableDisable);
01278     if (enableEnable || enableDisable)
01279         setEnableText(enableEnable);
01280 }
01281 
01282 /******************************************************************************
01283 *  Called when the mouse is clicked on the ListView.
01284 *  Deselects the current item and disables the actions if appropriate, or
01285 *  displays a context menu to modify or delete the selected item.
01286 */
01287 void MainWindow::slotMouseClicked(int button, QListViewItem* item, const QPoint& pt, int)
01288 {
01289     if (button == Qt::RightButton)
01290     {
01291         kdDebug(5950) << "MainWindow::slotMouseClicked(right)\n";
01292         if (mContextMenu)
01293             mContextMenu->popup(pt);
01294     }
01295     else if (!item)
01296     {
01297         kdDebug(5950) << "MainWindow::slotMouseClicked(left)\n";
01298         mListView->clearSelection();
01299         mActionCreateTemplate->setEnabled(false);
01300         mActionCopy->setEnabled(false);
01301         mActionModify->setEnabled(false);
01302         mActionView->setEnabled(false);
01303         mActionDelete->setEnabled(false);
01304         mActionReactivate->setEnabled(false);
01305         mActionEnable->setEnabled(false);
01306     }
01307 }
01308 
01309 /******************************************************************************
01310 *  Called when the mouse is double clicked on the ListView.
01311 *  Displays the Edit Alarm dialog, for the clicked item if applicable.
01312 */
01313 void MainWindow::slotDoubleClicked(QListViewItem* item)
01314 {
01315     kdDebug(5950) << "MainWindow::slotDoubleClicked()\n";
01316     if (item)
01317     {
01318         if (mListView->expired((AlarmListViewItem*)item))
01319             slotView();
01320         else
01321             slotModify();
01322     }
01323     else
01324         slotNew();
01325 }
01326 
01327 /******************************************************************************
01328 *  Set the text of the Enable/Disable menu action.
01329 */
01330 void MainWindow::setEnableText(bool enable)
01331 {
01332     mActionEnableEnable = enable;
01333     mActionEnable->setText(enable ? i18n("Ena&ble") : i18n("Disa&ble"));
01334 }
01335 
01336 /******************************************************************************
01337 *  Display or hide the specified main window.
01338 *  This should only be called when the application doesn't run in the system tray.
01339 */
01340 MainWindow* MainWindow::toggleWindow(MainWindow* win)
01341 {
01342     if (win  &&  mWindowList.find(win) != mWindowList.end())
01343     {
01344         // A window is specified (and it exists)
01345         if (win->isVisible())
01346         {
01347             // The window is visible, so close it
01348             win->close();
01349             return 0;
01350         }
01351         else
01352         {
01353             // The window is hidden, so display it
01354             win->hide();        // in case it's on a different desktop
01355             win->showNormal();
01356             win->raise();
01357             win->setActiveWindow();
01358             return win;
01359         }
01360     }
01361 
01362     // No window is specified, or the window doesn't exist. Open a new one.
01363     win = create();
01364     win->show();
01365     return win;
01366 }
KDE Home | KDE Accessibility Home | Description of Access Keys