00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00028 #include <kaboutapplication.h>
00029 #include <kstartupinfo.h>
00030 #include <kmessagebox.h>
00031 #include <kfiledialog.h>
00032 #include <kimagefilepreview.h>
00033 #include <kapplication.h>
00034 #include <krun.h>
00035
00036 #include <kdebug.h>
00037
00038 #include <qfile.h>
00039 #include <qtimer.h>
00040 #include <qpushbutton.h>
00041 #include <qlabel.h>
00042 #include <qpixmap.h>
00043
00044 #include "configuration.h"
00045 #include "configdialog.h"
00046 #include "wsystray.h"
00047 #include "kwebsnap.h"
00048 #include "kwebsnapwidget.h"
00049 #include "wshttpserver.h"
00050
00051 KWebSnap::KWebSnap(QWidget* parent, const char* name)
00052 : KMainWindow(parent, name),
00053 m_quit( FALSE ),
00054 m_secondsLeft(Config().Capture.CaptureDelay / 1000),
00055 m_scount(0),
00056 m_logger(this, "logger"),
00057 m_grabber(this, "grabber"),
00058 m_ftp(this, "ftp")
00059 {
00060 KStartupInfo::appStarted();
00061
00062 applyPreferences();
00063
00064 m_confDialog = 0;
00065 m_server = 0;
00066
00067
00068 m_mainWidget = new KWebSnapWidget( this, "mainWidget" );
00069 setCentralWidget(m_mainWidget);
00070
00071
00072 connect( m_mainWidget, SIGNAL( snapClicked() ), SLOT( slotSnapshot() ) );
00073 connect( m_mainWidget, SIGNAL( helpClicked() ), SLOT( slotHelp() ) );
00074 connect( m_mainWidget, SIGNAL( configClicked() ), SLOT( slotConfig() ) );
00075 connect( m_mainWidget, SIGNAL( aboutClicked() ), SLOT( slotAbout() ) );
00076 connect( m_mainWidget, SIGNAL( quitClicked() ), SLOT( slotQuit() ) );
00077 connect( m_mainWidget, SIGNAL( stopClicked() ), SLOT( slotStop() ) );
00078 connect( m_mainWidget, SIGNAL( startClicked() ), SLOT( slotStart() ) );
00079
00080
00081 m_systray = new WSysTray( this, "System Tray" );
00082
00083
00084 connect( &m_grabber, SIGNAL( startCapture() ), m_systray, SLOT( drawFlash() ) );
00085 connect( &m_grabber, SIGNAL( finishCapture() ), m_systray, SLOT( resetIcon() ) );
00086 connect( &m_grabber, SIGNAL( startSave() ), m_systray, SLOT( drawSave() ) );
00087 connect( &m_grabber, SIGNAL( finishSave() ), m_systray, SLOT( resetIcon() ) );
00088
00089
00090 connect( this, SIGNAL( timeLeft(int) ), m_systray, SLOT( setNumber(int) ) );
00091 connect( this, SIGNAL( start() ), m_systray, SLOT( disableMenues() ) );
00092 connect( this, SIGNAL( stop() ), m_systray, SLOT( enableMenues() ) );
00093 connect( this, SIGNAL( stop() ), m_systray, SLOT( resetIcon() ) );
00094
00095
00096 connect( &m_ftp, SIGNAL( startTransfer() ), m_systray, SLOT( drawSend() ) );
00097 connect( &m_ftp, SIGNAL( finishTransfer() ), m_systray, SLOT( resetIcon() ) );
00098 connect( &m_ftp, SIGNAL( message(const QString&)), m_mainWidget, SLOT(setLabel(const QString&)));
00099
00100
00101 connect( this, SIGNAL(message(const QString&, int)), m_mainWidget, SLOT(setLabel(const QString&)));
00102 connect( this, SIGNAL(message(const QString&, int)), &m_logger, SLOT(print(const QString&, int)));
00103
00104
00105 connect( &m_grabber, SIGNAL(message(const QString&, int)), m_mainWidget, SLOT(setLabel(const QString&)));
00106 connect( &m_grabber, SIGNAL(message(const QString&, int)), &m_logger, SLOT(print(const QString&, int)));
00107
00108
00109 connect( &m_ftp, SIGNAL( message(const QString&, int)), &m_logger, SLOT(print(const QString&, int)));
00110 connect( &m_ftp, SIGNAL( error(const QString&)), &m_logger, SLOT(printDebug(const QString&)));
00111
00112
00113 m_timer = new QTimer(this);
00114
00115
00116 m_mainWidget->setStop(false);
00117
00118 QPixmap snapshot = m_grabber.capture();
00119 updatePreview(snapshot);
00120
00121
00122 emit message(i18n("Welcome to WebSnap."));
00123 }
00124
00125 KWebSnap::~KWebSnap()
00126 {
00127 delete m_timer;
00128 m_timer = 0;
00129
00130 delete m_systray;
00131 m_systray = 0;
00132
00133 delete m_confDialog;
00134 m_confDialog = 0;
00135
00136 delete m_server;
00137 m_confDialog = 0;
00138 }
00139
00140 void KWebSnap::startTimer()
00141 {
00142 m_timer->start(1000, FALSE);
00143 }
00144
00145
00146 void KWebSnap::slotSnapshot()
00147 {
00148 QPixmap snapshot = m_grabber.capture();
00149 updatePreview(snapshot);
00150 m_grabber.saveFiles();
00151 }
00152
00153 void KWebSnap::updatePreview(const QPixmap& pixmap)
00154 {
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 m_mainWidget->setPreview(pixmap);
00168 }
00169
00170 void KWebSnap::slotHelp()
00171 {
00172 KRun* help = new KRun("http://websnap.sourceforge.net");
00173
00174
00175 }
00176
00177 void KWebSnap::slotConfig()
00178 {
00179
00180 if (m_confDialog==0)
00181 {
00182 m_confDialog=new ConfigDialog(this);
00183
00184 connect(m_confDialog, SIGNAL(settingsChanged()), this, SLOT(applyPreferences()));
00185 }
00186
00187 m_confDialog->updateDialog();
00188
00189 if (m_confDialog->exec()==QDialog::Accepted)
00190 {
00191 m_confDialog->updateConfiguration();
00192 applyPreferences();
00193 }
00194
00195 delete m_confDialog;
00196 m_confDialog = 0;
00197 }
00198
00199 void KWebSnap::slotQuit()
00200 {
00201 m_quit = true;
00202
00203 if(Config().Ftp.UseFtp && Config().Ftp.UseOffline)
00204 connect( &m_ftp, SIGNAL(finishTransfer()), this, SLOT(close()));
00205 else
00206 connect( this, SIGNAL(stop()), this, SLOT(close()));
00207
00208 slotStop();
00209 }
00210
00211 void KWebSnap::slotAbout()
00212 {
00213 KAboutApplication* kabout = new KAboutApplication(this);
00214 kabout->show();
00215 }
00216
00217 void KWebSnap::slotStop()
00218 {
00219 if(m_timer->isActive())
00220 {
00221 m_timer->stop();
00222 }
00223
00224 if(Config().Ftp.UseFtp)
00225 {
00226 m_ftp.stop();
00227
00228 if(Config().Ftp.UseOffline)
00229 m_ftp.sendOffline();
00230 }
00231
00232 if(Config().WWW.UseWWW)
00233 {
00234 if(m_server) disconnect( m_server, SIGNAL( message(const QString&, int)), &m_logger, SLOT(print(const QString&, int)));
00235 delete m_server;
00236 m_server = 0;
00237 }
00238
00239
00240 disconnect( m_timer, SIGNAL(timeout()), this, SLOT(slotOnTimer()) );
00241
00242 if(Config().Ftp.UseFtp) disconnect( &m_ftp, SIGNAL(finishTransfer()), this, SLOT(startTimer()));
00243 else disconnect( this, SIGNAL(timeout()), this, SLOT(startTimer()));
00244
00245
00246 m_mainWidget->setStop(false);
00247
00248
00249 m_mainWidget->setStart(true);
00250 m_mainWidget->setConfig(true);
00251
00252 emit stop();
00253 }
00254
00255 void KWebSnap::slotStart()
00256 {
00257 QString s;
00258 if(!Config().validate(s))
00259 {
00260 KMessageBox::information( this, s, i18n("Error") );
00261 return;
00262 }
00263
00264 connect( m_timer, SIGNAL(timeout()), this, SLOT(slotOnTimer()) );
00265
00266
00267 if(Config().Ftp.UseFtp)
00268 {
00269
00270
00271 connect( &m_ftp, SIGNAL(finishTransfer()), this, SLOT(startTimer()));
00272 }
00273 else
00274 {
00275 connect( this, SIGNAL(timeout()), this, SLOT(startTimer()));
00276 }
00277
00278 if(Config().WWW.UseWWW)
00279 {
00280 m_server = new WSHttpServer(this);
00281 if(m_server->ok())
00282 {
00283 connect( m_server, SIGNAL( message(const QString&, int)), &m_logger, SLOT(print(const QString&, int)));
00284 }
00285 else
00286 {
00287 QString msg = i18n("Could not bind to port %1.").arg(Config().WWW.Port);
00288 KMessageBox::sorry(this, msg, i18n("WWW Server Error"));
00289 emit message( msg );
00290 Config().WWW.UseWWW = false;
00291 delete m_server;
00292 m_server = 0;
00293 }
00294 }
00295
00296
00297 m_mainWidget->setStop(true);
00298
00299
00300 m_mainWidget->setStart(false);
00301 m_mainWidget->setConfig(false);
00302
00303
00304 m_secondsLeft = Config().Capture.CaptureDelay / 1000;
00305
00306
00307 m_scount = 0;
00308
00309 emit start();
00310
00311 startTimer();
00312 }
00313
00314 void KWebSnap::slotOnTimer()
00315 {
00316 m_secondsLeft--;
00317
00318 if(m_secondsLeft == 0)
00319 {
00320 m_timer->stop();
00321 slotSnapSend();
00322 m_secondsLeft = Config().Capture.CaptureDelay / 1000;
00323 emit timeout();
00324 }
00325 else if(m_secondsLeft < 16)
00326 {
00327 emit timeLeft(m_secondsLeft);
00328 }
00329 }
00330
00331 void KWebSnap::slotSnapSend()
00332 {
00333 m_scount++;
00334
00335 slotSnapshot();
00336
00337 if(Config().Ftp.UseFtp)
00338 {
00339 QString errorString;
00340 if(Config().validate(errorString))
00341 {
00342 m_ftp.sendFiles();
00343 }
00344 else
00345 emit error(errorString);
00346 }
00347 }
00348
00349
00350 bool KWebSnap::queryClose()
00351 {
00352 if (!m_quit)
00353 {
00354 KMessageBox::information( this, i18n("KWebSnap will still stay in the system tray.\nPress quit to exit."), i18n("Minimize KWebSnap"), "dont_show_again");
00355 hide();
00356 return FALSE;
00357 }
00358 else
00359 {
00360 return TRUE;
00361 }
00362 }
00363
00364 bool KWebSnap::close()
00365 {
00366 m_quit = TRUE;
00367 return KMainWindow::close();
00368 }
00369
00370 void KWebSnap::applyPreferences()
00371 {
00372 Config().write();
00373 emit message(i18n("wrote settings."), Configuration::Debug);
00374 }
00375