korganizer

alarmdockwindow.cpp
1/*
2 This file is part of KOrganizer.
3
4 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 As a special exception, permission is given to link this program
21 with any edition of TQt, and distribute the resulting executable,
22 without including the source code for TQt in the source distribution.
23*/
24
25#include "alarmdockwindow.h"
26#include "koalarmclient.h"
27
28#include <tdeapplication.h>
29#include <kdebug.h>
30#include <tdelocale.h>
31#include <kiconloader.h>
32#include <tdeconfig.h>
33#include <kurl.h>
34#include <tdestandarddirs.h>
35#include <dcopclient.h>
36#include <tdepopupmenu.h>
37#include <tdemessagebox.h>
38#include <tdeaction.h>
39#include <kstdaction.h>
40
41#include <tqtooltip.h>
42#include <tqfile.h>
43
44#include <stdlib.h>
45
46AlarmDockWindow::AlarmDockWindow( const char *name )
47 : KSystemTray( 0, name )
48{
49 // Read the autostart status from the config file
50 TDEConfig *config = tdeApp->config();
51 config->setGroup("General");
52 bool autostart = config->readBoolEntry( "Autostart", true );
53 bool alarmsEnabled = config->readBoolEntry( "Enabled", true );
54
55 mName = i18n( "KOrganizer Reminder Daemon" );
56 setCaption( mName );
57
58 // Set up icons
59 TDEGlobal::iconLoader()->addAppDir( "korgac" );
60 mPixmapEnabled = loadIcon( "korgac" );
61 mPixmapDisabled = loadIcon( "korgac_disabled" );
62
63 setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled );
64
65 // Set up the context menu
66 mSuspendAll = contextMenu()->insertItem( i18n("Suspend All"), this, TQ_SLOT( slotSuspendAll() ) );
67 mDismissAll = contextMenu()->insertItem( i18n("Dismiss All"), this, TQ_SLOT( slotDismissAll() ) );
68 contextMenu()->setItemEnabled( mSuspendAll, false );
69 contextMenu()->setItemEnabled( mDismissAll, false );
70
71 contextMenu()->insertSeparator();
72 mAlarmsEnabledId = contextMenu()->insertItem( i18n("Reminders Enabled"), this,
73 TQ_SLOT( toggleAlarmsEnabled() ) );
74 mAutostartId = contextMenu()->insertItem( i18n("Start Reminder Daemon at Login"), this,
75 TQ_SLOT( toggleAutostart() ) );
76 contextMenu()->setItemChecked( mAutostartId, autostart );
77 contextMenu()->setItemChecked( mAlarmsEnabledId, alarmsEnabled );
78
79 // Disable standard quit behaviour. We have to intercept the quit even, if the
80 // main window is hidden.
81 TDEActionCollection *ac = actionCollection();
82 const char *quitName = KStdAction::name( KStdAction::Quit );
83 TDEAction *quit = ac->action( quitName );
84 if ( !quit ) {
85 kdDebug(5890) << "No Quit standard action." << endl;
86 } else {
87 quit->disconnect( TQ_SIGNAL( activated() ), this,
88 TQ_SLOT( maybeQuit() ) );
89 connect( quit, TQ_SIGNAL( activated() ), TQ_SLOT( slotQuit() ) );
90 }
91
92 TQToolTip::add(this, mName );
93}
94
95AlarmDockWindow::~AlarmDockWindow()
96{
97}
98
99void AlarmDockWindow::resizeTrayIcon ()
100{
101 // Honor Free Desktop specifications that allow for arbitrary system tray icon sizes
102 mPixmapEnabled = loadSizedIcon( "korgac", width() );
103 mPixmapDisabled = loadSizedIcon( "korgac_disabled", width() );
104
105 TDEConfig *config = tdeApp->config();
106 bool alarmsEnabled = config->readBoolEntry( "Enabled", true );
107 setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled );
108}
109
110void AlarmDockWindow::resizeEvent ( TQResizeEvent * )
111{
112 resizeTrayIcon();
113}
114
115void AlarmDockWindow::showEvent ( TQShowEvent *se )
116{
117 resizeTrayIcon();
118 KSystemTray::showEvent(se);
119}
120
121void AlarmDockWindow::slotUpdate( int reminders )
122{
123 TQToolTip::remove( this );
124 if ( reminders > 0 )
125 {
126 TQToolTip::add( this, i18n( "There is 1 active reminder.",
127 "There are %n active reminders.", reminders ) );
128 contextMenu()->setItemEnabled( mSuspendAll, true );
129 contextMenu()->setItemEnabled( mDismissAll, true );
130 }
131 else
132 {
133 TQToolTip::add( this, mName );
134 contextMenu()->setItemEnabled( mSuspendAll, false );
135 contextMenu()->setItemEnabled( mDismissAll, false );
136 }
137}
138
139void AlarmDockWindow::toggleAlarmsEnabled()
140{
141 kdDebug(5890) << "AlarmDockWindow::toggleAlarmsEnabled()" << endl;
142
143 TDEConfig *config = tdeApp->config();
144 config->setGroup( "General" );
145
146 bool enabled = !contextMenu()->isItemChecked( mAlarmsEnabledId );
147 contextMenu()->setItemChecked( mAlarmsEnabledId, enabled );
148 setPixmap( enabled ? mPixmapEnabled : mPixmapDisabled );
149
150 config->writeEntry( "Enabled", enabled );
151 config->sync();
152}
153
154void AlarmDockWindow::toggleAutostart()
155{
156 bool autostart = !contextMenu()->isItemChecked( mAutostartId );
157
158 enableAutostart( autostart );
159}
160
161void AlarmDockWindow::slotSuspendAll()
162{
163 emit suspendAllSignal();
164}
165
166void AlarmDockWindow::slotDismissAll()
167{
168 emit dismissAllSignal();
169}
170
171void AlarmDockWindow::enableAutostart( bool enable )
172{
173 TDEConfig *config = tdeApp->config();
174 config->setGroup( "General" );
175 config->writeEntry( "Autostart", enable );
176 config->sync();
177
178 contextMenu()->setItemChecked( mAutostartId, enable );
179}
180
181void AlarmDockWindow::mousePressEvent( TQMouseEvent *e )
182{
183 if ( e->button() == TQt::LeftButton ) {
184 tdeApp->startServiceByDesktopName( "korganizer", TQString() );
185 } else {
186 KSystemTray::mousePressEvent( e );
187 }
188}
189
190//void AlarmDockWindow::closeEvent( TQCloseEvent * )
191void AlarmDockWindow::slotQuit()
192{
193 int result = KMessageBox::questionYesNoCancel( this,
194 i18n("Do you want to start the KOrganizer reminder daemon at login "
195 "(note that you will not get reminders whilst the daemon is not running)?"),
196 i18n("Close KOrganizer Reminder Daemon"),
197 i18n("Start"), i18n("Do Not Start"),
198 TQString::fromLatin1("AskForStartAtLogin")
199 );
200
201 bool autostart = true;
202 if ( result == KMessageBox::No ) autostart = false;
203 enableAutostart( autostart );
204
205 if ( result != KMessageBox::Cancel )
206 emit quitSignal();
207}
208
209#include "alarmdockwindow.moc"