00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <qdragobject.h>
00010 #include <qlineedit.h>
00011 #include <qprinter.h>
00012 #include <qprintdialog.h>
00013 #include <qpainter.h>
00014 #include <qpaintdevicemetrics.h>
00015
00016 #include <kglobal.h>
00017 #include <klocale.h>
00018 #include <kiconloader.h>
00019 #include <kmenubar.h>
00020 #include <kkeydialog.h>
00021 #include <kaccel.h>
00022 #include <kio/netaccess.h>
00023 #include <kfiledialog.h>
00024 #include <kconfig.h>
00025 #include <kurl.h>
00026 #include <kurlrequesterdlg.h>
00027 #include <kdebug.h>
00028 #include <kmessagebox.h>
00029 #include <kstddirs.h>
00030 #include <kedittoolbar.h>
00031 #include <kstdaccel.h>
00032 #include <kaction.h>
00033 #include <kstdaction.h>
00034
00035 #include "kandyprefsdialog.h"
00036 #include "commandscheduler.h"
00037 #include "kandyprefs.h"
00038 #include "modem.h"
00039
00040 #include "kandy.h"
00041 #include <kstatusbar.h>
00042 #include "kandy.moc"
00043
00044 Kandy::Kandy(CommandScheduler *scheduler)
00045 : KMainWindow( 0, "Kandy" ),
00046 mPrinter(0)
00047 {
00048 mScheduler = scheduler;
00049
00050 mPreferencesDialog = 0;
00051
00052 mView = new KandyView(mScheduler,this);
00053
00054
00055 setAcceptDrops(true);
00056
00057
00058 setCentralWidget(mView);
00059
00060
00061 setupActions();
00062
00063 statusBar()->insertItem(i18n(" Disconnected "),0,0,true);
00064
00065
00066 statusBar()->show();
00067
00068
00069 connect(mView, SIGNAL(signalChangeStatusbar(const QString&)),
00070 this, SLOT(changeStatusbar(const QString&)));
00071 connect(mView, SIGNAL(signalChangeCaption(const QString&)),
00072 this, SLOT(changeCaption(const QString&)));
00073
00074 connect(mView,SIGNAL(modifiedChanged(bool)),SLOT(setTitle()));
00075
00076 KConfig *config = KGlobal::config();
00077 config->setGroup("General");
00078 QString currentProfile = config->readEntry("CurrentProfile",
00079 locate("appdata","default.kandy"));
00080 if (!currentProfile.isEmpty()) load(currentProfile);
00081 }
00082
00083 Kandy::~Kandy()
00084 {
00085 }
00086
00087 void Kandy::load(const QString& filename)
00088 {
00089 if (!mView->loadFile(filename)) {
00090 KMessageBox::error(this,i18n("Could not load file %1").arg(filename));
00091 }
00092
00093 mFilename = filename;
00094 setTitle();
00095 }
00096
00097 void Kandy::save(const QString & filename)
00098 {
00099 if (!filename.isEmpty()) {
00100 if (!mView->saveFile(filename)) {
00101 KMessageBox::error(this,i18n("Couldn't save file %1.").arg(filename));
00102 } else {
00103 mFilename = filename;
00104 setTitle();
00105 }
00106 }
00107 }
00108
00109 void Kandy::setupActions()
00110 {
00111 KStdAction::open(this, SLOT(fileOpen()), actionCollection());
00112 KStdAction::save(this, SLOT(fileSave()), actionCollection());
00113 KStdAction::saveAs(this, SLOT(fileSaveAs()), actionCollection());
00114
00115 KStdAction::quit(this, SLOT(close()), actionCollection());
00116
00117 mToolbarAction = KStdAction::showToolbar(this, SLOT(optionsShowToolbar()), actionCollection());
00118 mStatusbarAction = KStdAction::showStatusbar(this, SLOT(optionsShowStatusbar()), actionCollection());
00119
00120 KStdAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
00121 KStdAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
00122 KStdAction::preferences(this, SLOT(optionsPreferences()), actionCollection());
00123
00124 new KAction(i18n("Mobile GUI"),0,this,SLOT(showMobileGui()),
00125 actionCollection(),"show_mobilegui");
00126
00127 mConnectAction = new KAction(i18n("Connect"),0,this,SLOT(modemConnect()),
00128 actionCollection(),"modem_connect");
00129 mDisconnectAction = new KAction(i18n("Disconnect"),0,this,
00130 SLOT(modemDisconnect()),actionCollection(),
00131 "modem_disconnect");
00132
00133 createGUI();
00134 }
00135
00136 void Kandy::saveProperties(KConfig *)
00137 {
00138
00139
00140
00141 }
00142
00143 void Kandy::readProperties(KConfig *)
00144 {
00145
00146
00147
00148
00149 }
00150
00151 void Kandy::dragEnterEvent(QDragEnterEvent *event)
00152 {
00153
00154 KMainWindow::dragEnterEvent(event);
00155
00156
00157
00158 }
00159
00160 void Kandy::dropEvent(QDropEvent *event)
00161 {
00162
00163
00164
00165
00166
00167 KMainWindow::dropEvent(event);
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 }
00183
00184 void Kandy::fileOpen()
00185 {
00186
00187
00188
00189 QString filename = KFileDialog::getOpenFileName();
00190 if (!filename.isEmpty()) load(filename);
00191 }
00192
00193 void Kandy::fileSave()
00194 {
00195 if (mFilename.isEmpty()) fileSaveAs();
00196 else save(mFilename);
00197 }
00198
00199 void Kandy::fileSaveAs()
00200 {
00201 QString filename = KFileDialog::getSaveFileName();
00202 save(filename);
00203 }
00204
00205 void Kandy::filePrint()
00206 {
00207
00208
00209
00210 if (!mPrinter) mPrinter = new QPrinter;
00211 if (QPrintDialog::getPrinterSetup(mPrinter))
00212 {
00213
00214
00215
00216 QPainter p;
00217 p.begin(mPrinter);
00218
00219
00220 QPaintDeviceMetrics metrics(mPrinter);
00221 mView->print(&p, metrics.height(), metrics.width());
00222
00223
00224 p.end();
00225 }
00226 }
00227
00228 void Kandy::optionsShowToolbar()
00229 {
00230
00231
00232 if (mToolbarAction->isChecked())
00233 toolBar()->show();
00234 else
00235 toolBar()->hide();
00236 }
00237
00238 void Kandy::optionsShowStatusbar()
00239 {
00240
00241
00242 if (mStatusbarAction->isChecked())
00243 statusBar()->show();
00244 else
00245 statusBar()->hide();
00246 }
00247
00248 void Kandy::optionsConfigureKeys()
00249 {
00250 KKeyDialog::configureKeys(actionCollection(), "kandyui.rc");
00251 }
00252
00253 void Kandy::optionsConfigureToolbars()
00254 {
00255
00256 KEditToolbar dlg(actionCollection());
00257 if (dlg.exec())
00258 {
00259
00260 createGUI();
00261 }
00262 }
00263
00264 void Kandy::optionsPreferences()
00265 {
00266 if (!mPreferencesDialog) {
00267 mPreferencesDialog = new KandyPrefsDialog(this);
00268 mPreferencesDialog->readConfig();
00269 }
00270
00271 mPreferencesDialog->show();
00272 mPreferencesDialog->raise();
00273 }
00274
00275 void Kandy::changeStatusbar(const QString& text)
00276 {
00277
00278 statusBar()->message(text);
00279 }
00280
00281 void Kandy::changeCaption(const QString& text)
00282 {
00283
00284 setCaption(text);
00285 }
00286
00287 void Kandy::setTitle()
00288 {
00289 if (mFilename.isEmpty()) {
00290 setCaption(i18n("New Profile"),mView->isModified());
00291 } else {
00292 setCaption(mFilename,mView->isModified());
00293 }
00294 }
00295
00296 bool Kandy::queryClose()
00297 {
00298 if (mView->isModified()) {
00299 switch (KMessageBox::warningYesNoCancel(this,
00300 i18n("Save changes to profile %1?").arg(mFilename))) {
00301 case KMessageBox::Yes :
00302 fileSave();
00303 return true;
00304 case KMessageBox::No :
00305 return true;
00306 default:
00307 return false;
00308 }
00309 } else {
00310 return true;
00311 }
00312 }
00313
00314 void Kandy::modemConnect()
00315 {
00316 if (!mScheduler->modem()->open()) {
00317 KMessageBox::sorry(this,
00318 i18n("Cannot open modem device %1.")
00319 .arg(KandyPrefs::instance()->mSerialDevice), i18n("Modem Error"));
00320 return;
00321 }
00322
00323 statusBar()->changeItem(i18n(" Connected "),0);
00324
00325 emit connectStateChanged(true);
00326 }
00327
00328 void Kandy::modemDisconnect()
00329 {
00330 mScheduler->modem()->close();
00331
00332 statusBar()->changeItem(i18n(" Disconnected "),0);
00333
00334 emit connectStateChanged(false);
00335 }
00336
00337 void Kandy::showMobileGui()
00338 {
00339 emit showMobileWin();
00340 }