alarmdockwindow.cpp
00001 /* 00002 This file is part of KOrganizer. 00003 00004 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> 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 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 00020 As a special exception, permission is given to link this program 00021 with any edition of TQt, and distribute the resulting executable, 00022 without including the source code for TQt in the source distribution. 00023 */ 00024 00025 #include "alarmdockwindow.h" 00026 #include "koalarmclient.h" 00027 00028 #include <kapplication.h> 00029 #include <kdebug.h> 00030 #include <kdeversion.h> 00031 #include <klocale.h> 00032 #include <kiconloader.h> 00033 #include <kconfig.h> 00034 #include <kurl.h> 00035 #include <kstandarddirs.h> 00036 #include <dcopclient.h> 00037 #include <kpopupmenu.h> 00038 #include <kmessagebox.h> 00039 #include <kaction.h> 00040 #include <kstdaction.h> 00041 00042 #include <tqtooltip.h> 00043 #include <tqfile.h> 00044 00045 #include <stdlib.h> 00046 00047 AlarmDockWindow::AlarmDockWindow( const char *name ) 00048 : KSystemTray( 0, name ) 00049 { 00050 // Read the autostart status from the config file 00051 KConfig *config = kapp->config(); 00052 config->setGroup("General"); 00053 bool autostart = config->readBoolEntry( "Autostart", true ); 00054 bool alarmsEnabled = config->readBoolEntry( "Enabled", true ); 00055 00056 mName = i18n( "KOrganizer Reminder Daemon" ); 00057 setCaption( mName ); 00058 00059 // Set up icons 00060 KGlobal::iconLoader()->addAppDir( "korgac" ); 00061 mPixmapEnabled = loadSizedIcon( "korgac", width() ); 00062 mPixmapDisabled = loadSizedIcon( "korgac_disabled", width() ); 00063 00064 setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled ); 00065 00066 // Set up the context menu 00067 mSuspendAll = contextMenu()->insertItem( i18n("Suspend All"), this, TQT_SLOT( slotSuspendAll() ) ); 00068 mDismissAll = contextMenu()->insertItem( i18n("Dismiss All"), this, TQT_SLOT( slotDismissAll() ) ); 00069 contextMenu()->setItemEnabled( mSuspendAll, false ); 00070 contextMenu()->setItemEnabled( mDismissAll, false ); 00071 00072 contextMenu()->insertSeparator(); 00073 mAlarmsEnabledId = contextMenu()->insertItem( i18n("Reminders Enabled"), this, 00074 TQT_SLOT( toggleAlarmsEnabled() ) ); 00075 mAutostartId = contextMenu()->insertItem( i18n("Start Reminder Daemon at Login"), this, 00076 TQT_SLOT( toggleAutostart() ) ); 00077 contextMenu()->setItemChecked( mAutostartId, autostart ); 00078 contextMenu()->setItemChecked( mAlarmsEnabledId, alarmsEnabled ); 00079 00080 // Disable standard quit behaviour. We have to intercept the quit even, if the 00081 // main window is hidden. 00082 KActionCollection *ac = actionCollection(); 00083 const char *quitName = KStdAction::name( KStdAction::Quit ); 00084 KAction *quit = ac->action( quitName ); 00085 if ( !quit ) { 00086 kdDebug(5890) << "No Quit standard action." << endl; 00087 } else { 00088 #if KDE_IS_VERSION(3,3,90) 00089 quit->disconnect( TQT_SIGNAL( activated() ), this, 00090 TQT_SLOT( maybeQuit() ) ); 00091 connect( quit, TQT_SIGNAL( activated() ), TQT_SLOT( slotQuit() ) ); 00092 } 00093 #else //FIXME: remove for KDE 4.0 00094 quit->disconnect( TQT_SIGNAL( activated() ), tqApp, 00095 TQT_SLOT( closeAllWindows() ) ); 00096 } 00097 connect( this, TQT_SIGNAL( quitSelected() ), TQT_SLOT( slotQuit() ) ); 00098 #endif 00099 00100 TQToolTip::add(this, mName ); 00101 } 00102 00103 AlarmDockWindow::~AlarmDockWindow() 00104 { 00105 } 00106 00107 void AlarmDockWindow::resizeEvent ( TQResizeEvent * ) 00108 { 00109 // Honor Free Desktop specifications that allow for arbitrary system tray icon sizes 00110 mPixmapEnabled = loadSizedIcon( "korgac", width() ); 00111 mPixmapDisabled = loadSizedIcon( "korgac_disabled", width() ); 00112 00113 KConfig *config = kapp->config(); 00114 bool alarmsEnabled = config->readBoolEntry( "Enabled", true ); 00115 setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled ); 00116 } 00117 00118 void AlarmDockWindow::slotUpdate( int reminders ) 00119 { 00120 TQToolTip::remove( this ); 00121 if ( reminders > 0 ) 00122 { 00123 TQToolTip::add( this, i18n( "There is 1 active reminder.", 00124 "There are %n active reminders.", reminders ) ); 00125 contextMenu()->setItemEnabled( mSuspendAll, true ); 00126 contextMenu()->setItemEnabled( mDismissAll, true ); 00127 } 00128 else 00129 { 00130 TQToolTip::add( this, mName ); 00131 contextMenu()->setItemEnabled( mSuspendAll, false ); 00132 contextMenu()->setItemEnabled( mDismissAll, false ); 00133 } 00134 } 00135 00136 void AlarmDockWindow::toggleAlarmsEnabled() 00137 { 00138 kdDebug(5890) << "AlarmDockWindow::toggleAlarmsEnabled()" << endl; 00139 00140 KConfig *config = kapp->config(); 00141 config->setGroup( "General" ); 00142 00143 bool enabled = !contextMenu()->isItemChecked( mAlarmsEnabledId ); 00144 contextMenu()->setItemChecked( mAlarmsEnabledId, enabled ); 00145 setPixmap( enabled ? mPixmapEnabled : mPixmapDisabled ); 00146 00147 config->writeEntry( "Enabled", enabled ); 00148 config->sync(); 00149 } 00150 00151 void AlarmDockWindow::toggleAutostart() 00152 { 00153 bool autostart = !contextMenu()->isItemChecked( mAutostartId ); 00154 00155 enableAutostart( autostart ); 00156 } 00157 00158 void AlarmDockWindow::slotSuspendAll() 00159 { 00160 emit suspendAllSignal(); 00161 } 00162 00163 void AlarmDockWindow::slotDismissAll() 00164 { 00165 emit dismissAllSignal(); 00166 } 00167 00168 void AlarmDockWindow::enableAutostart( bool enable ) 00169 { 00170 KConfig *config = kapp->config(); 00171 config->setGroup( "General" ); 00172 config->writeEntry( "Autostart", enable ); 00173 config->sync(); 00174 00175 contextMenu()->setItemChecked( mAutostartId, enable ); 00176 } 00177 00178 void AlarmDockWindow::mousePressEvent( TQMouseEvent *e ) 00179 { 00180 if ( e->button() == Qt::LeftButton ) { 00181 kapp->startServiceByDesktopName( "korganizer", TQString() ); 00182 } else { 00183 KSystemTray::mousePressEvent( e ); 00184 } 00185 } 00186 00187 //void AlarmDockWindow::closeEvent( TQCloseEvent * ) 00188 void AlarmDockWindow::slotQuit() 00189 { 00190 int result = KMessageBox::questionYesNoCancel( this, 00191 i18n("Do you want to start the KOrganizer reminder daemon at login " 00192 "(note that you will not get reminders whilst the daemon is not running)?"), 00193 i18n("Close KOrganizer Reminder Daemon"), 00194 i18n("Start"), i18n("Do Not Start"), 00195 TQString::fromLatin1("AskForStartAtLogin") 00196 ); 00197 00198 bool autostart = true; 00199 if ( result == KMessageBox::No ) autostart = false; 00200 enableAutostart( autostart ); 00201 00202 if ( result != KMessageBox::Cancel ) 00203 emit quitSignal(); 00204 } 00205 00206 #include "alarmdockwindow.moc"