kandy

kandy.cpp
1/*
2 This file is part of Kandy.
3
4 Copyright (c) 2000,2001,2002 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 <tqlineedit.h>
26#include <tqprinter.h>
27#include <tqprintdialog.h>
28#include <tqpainter.h>
29#include <tqpaintdevicemetrics.h>
30
31#include <tdeglobal.h>
32#include <tdelocale.h>
33#include <kiconloader.h>
34#include <tdemenubar.h>
35#include <kkeydialog.h>
36#include <tdeaccel.h>
37#include <tdeio/netaccess.h>
38#include <tdefiledialog.h>
39#include <tdeconfig.h>
40#include <kurl.h>
41#include <kurlrequesterdlg.h>
42#include <kdebug.h>
43#include <tdemessagebox.h>
44#include <tdestandarddirs.h>
45#include <kedittoolbar.h>
46#include <tdestdaccel.h>
47#include <tdeaction.h>
48#include <kstdaction.h>
49#include <kurldrag.h>
50
51#include "kandyprefsdialog.h"
52#include "commandscheduler.h"
53#include "kandyprefs.h"
54#include "modem.h"
55
56#include "kandy.h"
57#include <kstatusbar.h>
58#include "kandy.moc"
59
60Kandy::Kandy(CommandScheduler *scheduler)
61 : TDEMainWindow( 0, "Kandy" ),
62 mPrinter(0)
63{
64 mScheduler = scheduler;
65
66 mPreferencesDialog = 0;
67
68 mView = new KandyView(mScheduler,this);
69
70 // accept dnd
71 setAcceptDrops(true);
72
73 // tell the TDEMainWindow that this is indeed the main widget
74 setCentralWidget(mView);
75
76 // then, setup our actions
77 setupActions();
78
79 statusBar()->insertItem(i18n(" Disconnected "),0,0,true);
80
81 setAutoSaveSettings();
82
83 // allow the view to change the statusbar and caption
84 connect(mView, TQ_SIGNAL(signalChangeStatusbar(const TQString&)),
85 this, TQ_SLOT(changeStatusbar(const TQString&)));
86 connect(mView, TQ_SIGNAL(signalChangeCaption(const TQString&)),
87 this, TQ_SLOT(changeCaption(const TQString&)));
88
89 connect(mView,TQ_SIGNAL(modifiedChanged(bool)),TQ_SLOT(setTitle()));
90
91 TDEConfig *config = TDEGlobal::config();
92 config->setGroup("General");
93 TQString currentProfile = config->readEntry("CurrentProfile",
94 locate("appdata","default.kandy"));
95 if (!currentProfile.isEmpty()) load(currentProfile);
96}
97
99{
100}
101
102void Kandy::load(const TQString& filename)
103{
104 if (!mView->loadFile(filename)) {
105 KMessageBox::error(this,i18n("Could not load file %1").arg(filename));
106 }
107
108 mFilename = filename;
109 setTitle();
110}
111
112void Kandy::save(const TQString & filename)
113{
114 if (!filename.isEmpty()) {
115 if (!mView->saveFile(filename)) {
116 KMessageBox::error(this,i18n("Could not save file %1.").arg(filename));
117 } else {
118 mFilename = filename;
119 setTitle();
120 }
121 }
122}
123
124void Kandy::setupActions()
125{
126 KStdAction::open(this, TQ_SLOT(fileOpen()), actionCollection());
127 KStdAction::save(this, TQ_SLOT(fileSave()), actionCollection());
128 KStdAction::saveAs(this, TQ_SLOT(fileSaveAs()), actionCollection());
129// KStdAction::print(this, TQ_SLOT(filePrint()), actionCollection());
130 KStdAction::quit(this, TQ_SLOT(close()), actionCollection());
131
132 createStandardStatusBarAction();
133 setStandardToolBarMenuEnabled(true);
134
135 KStdAction::keyBindings(this, TQ_SLOT(optionsConfigureKeys()), actionCollection());
136 KStdAction::configureToolbars(this, TQ_SLOT(optionsConfigureToolbars()), actionCollection());
137 KStdAction::preferences(this, TQ_SLOT(optionsPreferences()), actionCollection());
138
139 new TDEAction(i18n("Mobile GUI"),0,this,TQ_SLOT(showMobileGui()),
140 actionCollection(),"show_mobilegui");
141
142 mConnectAction = new TDEAction(i18n("Connect"),0,this,TQ_SLOT(modemConnect()),
143 actionCollection(),"modem_connect");
144 mDisconnectAction = new TDEAction(i18n("Disconnect"),0,this,
145 TQ_SLOT(modemDisconnect()),actionCollection(),
146 "modem_disconnect");
147
148 createGUI();
149}
150
151void Kandy::saveProperties(TDEConfig */*config*/)
152{
153 // the 'config' object points to the session managed
154 // config file. anything you write here will be available
155 // later when this app is restored
156}
157
158void Kandy::readProperties(TDEConfig */*config*/)
159{
160 // the 'config' object points to the session managed
161 // config file. this function is automatically called whenever
162 // the app is being restored. read in here whatever you wrote
163 // in 'saveProperties'
164}
165
166void Kandy::dragEnterEvent(TQDragEnterEvent *event)
167{
168 // do nothing
169 TDEMainWindow::dragEnterEvent(event);
170
171 // accept uri drops only
172// event->accept(KURLDrag::canDecode(event));
173}
174
175void Kandy::dropEvent(TQDropEvent *event)
176{
177 // this is a very simplistic implementation of a drop event. we
178 // will only accept a dropped URL. the TQt dnd code can do *much*
179 // much more, so please read the docs there
180
181 // do nothing
182 TDEMainWindow::dropEvent(event);
183/*
184 KURL::List list;
185
186 // see if we can decode a URI.. if not, just ignore it
187 if (KURLDrag::decode(event, list) && !list.isEmpty())
188 {
189 // okay, we have a URI.. process it
190 const KURL &url = uri.first();
191
192 if (url.isLocalFile())
193 {
194 // load in the file
195 load(url.path());
196 }
197 }
198*/
199}
200
201void Kandy::fileOpen()
202{
203 // this slot is called whenever the File->Open menu is selected,
204 // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
205 // button is clicked
206 TQString filename = KFileDialog::getOpenFileName();
207 if (!filename.isEmpty()) load(filename);
208}
209
210void Kandy::fileSave()
211{
212 if (mFilename.isEmpty()) fileSaveAs();
213 else save(mFilename);
214}
215
216void Kandy::fileSaveAs()
217{
218 TQString filename = KFileDialog::getSaveFileName();
219 save(filename);
220}
221
222void Kandy::filePrint()
223{
224 // this slot is called whenever the File->Print menu is selected,
225 // the Print shortcut is pressed (usually CTRL+P) or the Print toolbar
226 // button is clicked
227 if (!mPrinter) mPrinter = new TQPrinter;
228 if (TQPrintDialog::getPrinterSetup(mPrinter))
229 {
230 // setup the printer. with TQt, you always "print" to a
231 // TQPainter.. whether the output medium is a pixmap, a screen,
232 // or paper
233 TQPainter p;
234 p.begin(mPrinter);
235
236 // we let our view do the actual printing
237 TQPaintDeviceMetrics metrics(mPrinter);
238 mView->print(&p, metrics.height(), metrics.width());
239
240 // and send the result to the printer
241 p.end();
242 }
243}
244
245void Kandy::optionsConfigureKeys()
246{
247 KKeyDialog::configure( actionCollection(), this );
248}
249
250void Kandy::optionsConfigureToolbars()
251{
252 // use the standard toolbar editor
253 saveMainWindowSettings( TDEGlobal::config(), autoSaveGroup() );
254 KEditToolbar dlg(actionCollection());
255 connect(&dlg, TQ_SIGNAL(newToolbarConfig()), this, TQ_SLOT(newToolbarConfig()));
256 dlg.exec();
257}
258
259void Kandy::newToolbarConfig()
260{
261 // this slot is called when user clicks "Ok" or "Apply" in the toolbar editor.
262 // recreate our GUI, and re-apply the settings (e.g. "text under icons", etc.)
263 createGUI();
264 applyMainWindowSettings( TDEGlobal::config(), autoSaveGroup() );
265}
266
267void Kandy::optionsPreferences()
268{
269 if (!mPreferencesDialog) {
270 mPreferencesDialog = new KandyPrefsDialog(this);
271 mPreferencesDialog->readConfig();
272 }
273
274 mPreferencesDialog->show();
275 mPreferencesDialog->raise();
276}
277
278void Kandy::changeStatusbar(const TQString& text)
279{
280 // display the text on the statusbar
281 statusBar()->message(text);
282}
283
284void Kandy::changeCaption(const TQString& text)
285{
286 // display the text on the caption
287 setCaption(text);
288}
289
290void Kandy::setTitle()
291{
292 if (mFilename.isEmpty()) {
293 setCaption(i18n("New Profile"),mView->isModified());
294 } else {
295 setCaption(mFilename,mView->isModified());
296 }
297}
298
299bool Kandy::queryClose()
300{
301 if (mView->isModified()) {
302 switch (KMessageBox::warningYesNoCancel(this,
303 i18n("Save changes to profile %1?").arg(mFilename), TQString(), KStdGuiItem::save(), KStdGuiItem::discard())) {
304 case KMessageBox::Yes :
305 fileSave();
306 return true;
307 case KMessageBox::No :
308 return true;
309 default: // cancel
310 return false;
311 }
312 } else {
313 return true;
314 }
315}
316
317void Kandy::modemConnect()
318{
319 if (!mScheduler->modem()->open()) {
320 KMessageBox::sorry(this,
321 i18n("Cannot open modem device %1.")
322 .arg(KandyPrefs::serialDevice()), i18n("Modem Error"));
323 return;
324 }
325
326 statusBar()->changeItem(i18n(" Connected "),0);
327
328 emit connectStateChanged(true);
329}
330
331void Kandy::modemDisconnect()
332{
333 mScheduler->modem()->close();
334
335 statusBar()->changeItem(i18n(" Disconnected "),0);
336
337 emit connectStateChanged(false);
338}
339
340void Kandy::showMobileGui()
341{
342 emit showMobileWin();
343}
344
345void Kandy::showErrorMessage( const TQString &text )
346{
347 KMessageBox::error( 0, text );
348}
Dialog to change the kandy configuration.
This is the main view class for Kandy.
Definition: kandyview.h:52
void print(TQPainter *, int height, int width)
Print this view to any medium – paper or not.
Definition: kandyview.cpp:151
virtual ~Kandy()
Default Destructor.
Definition: kandy.cpp:98
void saveProperties(TDEConfig *)
This function is called when it is time for the app to save its properties for session management pur...
Definition: kandy.cpp:151
void load(const TQString &url)
Use this method to load whatever file/URL you have.
Definition: kandy.cpp:102
Kandy(CommandScheduler *)
Default Constructor.
Definition: kandy.cpp:60
void readProperties(TDEConfig *)
This function is called when this app is restored.
Definition: kandy.cpp:158
virtual void dragEnterEvent(TQDragEnterEvent *event)
Overridden virtuals for TQt drag 'n drop (XDND)
Definition: kandy.cpp:166