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 <tdeversion.h>
31#include <tdelocale.h>
32#include <kiconloader.h>
33#include <tdeconfig.h>
34#include <kurl.h>
35#include <tdestandarddirs.h>
36#include <dcopclient.h>
37#include <tdepopupmenu.h>
38#include <tdemessagebox.h>
39#include <tdeaction.h>
40#include <kstdaction.h>
41
42#include <tqtooltip.h>
43#include <tqfile.h>
44
45#include <stdlib.h>
46
47AlarmDockWindow::AlarmDockWindow( const char *name )
48 : KSystemTray( 0, name )
49{
50 // Read the autostart status from the config file
51 TDEConfig *config = tdeApp->config();
52 config->setGroup("General");
53 bool autostart = config->readBoolEntry( "Autostart", true );
54 bool alarmsEnabled = config->readBoolEntry( "Enabled", true );
55
56 mName = i18n( "KOrganizer Reminder Daemon" );
57 setCaption( mName );
58
59 // Set up icons
60 TDEGlobal::iconLoader()->addAppDir( "korgac" );
61 mPixmapEnabled = loadIcon( "korgac" );
62 mPixmapDisabled = loadIcon( "korgac_disabled" );
63
64 setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled );
65
66 // Set up the context menu
67 mSuspendAll = contextMenu()->insertItem( i18n("Suspend All"), this, TQ_SLOT( slotSuspendAll() ) );
68 mDismissAll = contextMenu()->insertItem( i18n("Dismiss All"), this, TQ_SLOT( slotDismissAll() ) );
69 contextMenu()->setItemEnabled( mSuspendAll, false );
70 contextMenu()->setItemEnabled( mDismissAll, false );
71
72 contextMenu()->insertSeparator();
73 mAlarmsEnabledId = contextMenu()->insertItem( i18n("Reminders Enabled"), this,
74 TQ_SLOT( toggleAlarmsEnabled() ) );
75 mAutostartId = contextMenu()->insertItem( i18n("Start Reminder Daemon at Login"), this,
76 TQ_SLOT( toggleAutostart() ) );
77 contextMenu()->setItemChecked( mAutostartId, autostart );
78 contextMenu()->setItemChecked( mAlarmsEnabledId, alarmsEnabled );
79
80 // Disable standard quit behaviour. We have to intercept the quit even, if the
81 // main window is hidden.
82 TDEActionCollection *ac = actionCollection();
83 const char *quitName = KStdAction::name( KStdAction::Quit );
84 TDEAction *quit = ac->action( quitName );
85 if ( !quit ) {
86 kdDebug(5890) << "No Quit standard action." << endl;
87 } else {
88#if KDE_IS_VERSION(3,3,90)
89 quit->disconnect( TQ_SIGNAL( activated() ), this,
90 TQ_SLOT( maybeQuit() ) );
91 connect( quit, TQ_SIGNAL( activated() ), TQ_SLOT( slotQuit() ) );
92 }
93#else //FIXME: remove for KDE 4.0
94 quit->disconnect( TQ_SIGNAL( activated() ), tqApp,
95 TQ_SLOT( closeAllWindows() ) );
96 }
97 connect( this, TQ_SIGNAL( quitSelected() ), TQ_SLOT( slotQuit() ) );
98#endif
99
100 TQToolTip::add(this, mName );
101}
102
103AlarmDockWindow::~AlarmDockWindow()
104{
105}
106
107void AlarmDockWindow::resizeTrayIcon ()
108{
109 // Honor Free Desktop specifications that allow for arbitrary system tray icon sizes
110 mPixmapEnabled = loadSizedIcon( "korgac", width() );
111 mPixmapDisabled = loadSizedIcon( "korgac_disabled", width() );
112
113 TDEConfig *config = tdeApp->config();
114 bool alarmsEnabled = config->readBoolEntry( "Enabled", true );
115 setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled );
116}
117
118void AlarmDockWindow::resizeEvent ( TQResizeEvent * )
119{
120 resizeTrayIcon();
121}
122
123void AlarmDockWindow::showEvent ( TQShowEvent *se )
124{
125 resizeTrayIcon();
126 KSystemTray::showEvent(se);
127}
128
129void AlarmDockWindow::slotUpdate( int reminders )
130{
131 TQToolTip::remove( this );
132 if ( reminders > 0 )
133 {
134 TQToolTip::add( this, i18n( "There is 1 active reminder.",
135 "There are %n active reminders.", reminders ) );
136 contextMenu()->setItemEnabled( mSuspendAll, true );
137 contextMenu()->setItemEnabled( mDismissAll, true );
138 }
139 else
140 {
141 TQToolTip::add( this, mName );
142 contextMenu()->setItemEnabled( mSuspendAll, false );
143 contextMenu()->setItemEnabled( mDismissAll, false );
144 }
145}
146
147void AlarmDockWindow::toggleAlarmsEnabled()
148{
149 kdDebug(5890) << "AlarmDockWindow::toggleAlarmsEnabled()" << endl;
150
151 TDEConfig *config = tdeApp->config();
152 config->setGroup( "General" );
153
154 bool enabled = !contextMenu()->isItemChecked( mAlarmsEnabledId );
155 contextMenu()->setItemChecked( mAlarmsEnabledId, enabled );
156 setPixmap( enabled ? mPixmapEnabled : mPixmapDisabled );
157
158 config->writeEntry( "Enabled", enabled );
159 config->sync();
160}
161
162void AlarmDockWindow::toggleAutostart()
163{
164 bool autostart = !contextMenu()->isItemChecked( mAutostartId );
165
166 enableAutostart( autostart );
167}
168
169void AlarmDockWindow::slotSuspendAll()
170{
171 emit suspendAllSignal();
172}
173
174void AlarmDockWindow::slotDismissAll()
175{
176 emit dismissAllSignal();
177}
178
179void AlarmDockWindow::enableAutostart( bool enable )
180{
181 TDEConfig *config = tdeApp->config();
182 config->setGroup( "General" );
183 config->writeEntry( "Autostart", enable );
184 config->sync();
185
186 contextMenu()->setItemChecked( mAutostartId, enable );
187}
188
189void AlarmDockWindow::mousePressEvent( TQMouseEvent *e )
190{
191 if ( e->button() == TQt::LeftButton ) {
192 tdeApp->startServiceByDesktopName( "korganizer", TQString() );
193 } else {
194 KSystemTray::mousePressEvent( e );
195 }
196}
197
198//void AlarmDockWindow::closeEvent( TQCloseEvent * )
199void AlarmDockWindow::slotQuit()
200{
201 int result = KMessageBox::questionYesNoCancel( this,
202 i18n("Do you want to start the KOrganizer reminder daemon at login "
203 "(note that you will not get reminders whilst the daemon is not running)?"),
204 i18n("Close KOrganizer Reminder Daemon"),
205 i18n("Start"), i18n("Do Not Start"),
206 TQString::fromLatin1("AskForStartAtLogin")
207 );
208
209 bool autostart = true;
210 if ( result == KMessageBox::No ) autostart = false;
211 enableAutostart( autostart );
212
213 if ( result != KMessageBox::Cancel )
214 emit quitSignal();
215}
216
217#include "alarmdockwindow.moc"