Main Page | Class List | File List | Class Members | File Members | Related Pages

kwebsnap.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Bojan D.                                        *
00003  *   bojan_d@users.sourceforge.net                                         *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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         // create the central widget
00068         m_mainWidget = new KWebSnapWidget( this, "mainWidget" );
00069         setCentralWidget(m_mainWidget);
00070         
00071         // connect the main widget signals so we handle them here
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         // create the system tray
00081         m_systray = new WSysTray( this, "System Tray" );
00082         
00083         // connect the grabber's signals to system tray
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         // connect our signals to system tray
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         // connect ftp signals
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         // connect the message signals
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         // connect grabber's messages
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         // connect ftp's messages
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         // create what we need
00113         m_timer = new QTimer(this);
00114         
00115         // set the stop button to disabled
00116         m_mainWidget->setStop(false);
00117 
00118         QPixmap snapshot = m_grabber.capture(); 
00119         updatePreview(snapshot);
00120                 
00121         // emit the message
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         /*QImage img = m_snapshot.convertToImage();
00156     double r1 = ((double) m_snapshot.height() ) / m_snapshot.width();
00157     if ( r1 * m_mainWidget->previewWidth()  < m_mainWidget->previewHeight() )
00158                 img = img.smoothScale(  m_mainWidget->previewWidth(), 
00159                                                                 int(m_mainWidget->previewWidth() * r1 ));
00160     else
00161                 img = img.smoothScale(  (int) (((double)m_mainWidget->previewHeight()) / r1),
00162                                                         (m_mainWidget->previewHeight()) );
00163 
00164     QPixmap pm;
00165     pm.convertFromImage( img );
00166     m_mainWidget->setPreview(pm);   */
00167         m_mainWidget->setPreview(pixmap);   
00168 }
00169 
00170 void KWebSnap::slotHelp()
00171 {
00172         KRun* help = new KRun("http://websnap.sourceforge.net");
00173         //      nothing more to do
00174         //      it autoruns, and subsequently deletes itself when done.
00175 }
00176 
00177 void KWebSnap::slotConfig()
00178 {
00179         // create dialog on demand
00180     if (m_confDialog==0)
00181         {
00182         m_confDialog=new ConfigDialog(this);    // create dialog on demand
00183                 // connect to the "settingsChanged" signal
00184         connect(m_confDialog, SIGNAL(settingsChanged()), this, SLOT(applyPreferences()));
00185         }
00186         
00187         m_confDialog->updateDialog();                           // update the dialog widgets
00188         
00189     if (m_confDialog->exec()==QDialog::Accepted)// execute the dialog
00190         {
00191                 m_confDialog->updateConfiguration();    // store settings in config object
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         // disconnect signals
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         // set the stop button disnabled
00246         m_mainWidget->setStop(false);
00247 
00248         // and start and config buttons to enabled
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         // setup timer control
00267         if(Config().Ftp.UseFtp)
00268         {
00269                 // connect the timer signal, so that we restart the countdown when the thumbnail image was transfered.
00270                 // this means we have to transfer thumbnail image last
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         // set the stop button enabled
00297         m_mainWidget->setStop(true);
00298 
00299         // and start and config buttons to disabled
00300         m_mainWidget->setStart(false);
00301         m_mainWidget->setConfig(false);
00302 
00303         // timer
00304         m_secondsLeft = Config().Capture.CaptureDelay / 1000;
00305 
00306         // session counter
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 

Generated on Mon Sep 5 12:52:33 2005 for kwebsnap.kdevelop by  doxygen 1.3.9.1