kpilot Library API Documentation

logWidget.cc

00001 /* logWidget.cc                         KPilot
00002 **
00003 ** Copyright (C) 2001 by Dan Pilone
00004 **
00005 ** This file defines the log window widget, which logs
00006 ** sync-messages during a HotSync.
00007 */
00008 
00009 /*
00010 ** This program is free software; you can redistribute it and/or modify
00011 ** it under the terms of the GNU General Public License as published by
00012 ** the Free Software Foundation; either version 2 of the License, or
00013 ** (at your option) any later version.
00014 **
00015 ** This program is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018 ** GNU General Public License for more details.
00019 **
00020 ** You should have received a copy of the GNU General Public License
00021 ** along with this program in a file called COPYING; if not, write to
00022 ** the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00023 ** MA 02111-1307, USA.
00024 */
00025 
00026 /*
00027 ** Bug reports and questions can be sent to kde-pim@kde.org.
00028 */
00029 static const char *logw_id =
00030         "$Id: logWidget.cc,v 1.28.2.5 2003/03/12 23:31:14 adridg Exp $";
00031 
00032 #include "options.h"
00033 
00034 #include <qfile.h>
00035 #include <qlayout.h>
00036 #include <qtextedit.h>
00037 #include <qwhatsthis.h>
00038 #include <qdatetime.h>
00039 #include <qlabel.h>
00040 #include <qpixmap.h>
00041 #include <qtimer.h>
00042 #include <qpushbutton.h>
00043 #include <qhbox.h>
00044 #include <qtextstream.h>
00045 #include <qpainter.h>
00046 
00047 #include <kglobal.h>
00048 #include <kstandarddirs.h>
00049 #include <kprogress.h>
00050 #include <kfiledialog.h>
00051 #include <kmessagebox.h>
00052 
00053 #include <pi-version.h>
00054 
00055 #ifndef PILOT_LINK_PATCH
00056 #define PILOT_LINK_PATCH "unknown"
00057 #endif
00058 
00059 #include "logWidget.moc"
00060 
00061 #if QT_VERSION < 0x030100
00062 #define TE_EOL "<br/>"
00063 #else
00064 #define TE_EOL "\n"
00065 #endif
00066 
00067 
00068 LogWidget::LogWidget(QWidget * parent) :
00069         PilotComponent(parent, "component_log", QString::null),
00070         DCOPObject("LogIface"),
00071         fLog(0L),
00072         fShowTime(false),
00073         fSplash(0L),
00074         fLabel(0L),
00075         fProgress(0L),
00076         fButtonBox(0L)
00077 {
00078         FUNCTIONSETUP;
00079         QGridLayout *grid = new QGridLayout(this, 4, 4, SPACING);
00080 
00081         grid->addRowSpacing(0, SPACING);
00082         grid->addRowSpacing(1, 100);
00083         grid->addColSpacing(2, 100);
00084         grid->addRowSpacing(3, SPACING);
00085         grid->addColSpacing(0, SPACING);
00086         grid->addColSpacing(3, SPACING);
00087         grid->setRowStretch(1, 50);
00088         grid->setColStretch(2, 50);
00089 
00090         fLog = new QTextEdit(this);
00091         fLog->setReadOnly(true);
00092 #if QT_VERSION < 0x030100
00093         /* nothing, use AutoText */
00094 #else
00095         fLog->setTextFormat(Qt::LogText);
00096 #endif
00097         
00098         QWhatsThis::add(fLog, i18n("<qt>This lists all the messages received "
00099                         "during the current HotSync</qt>"));
00100         grid->addMultiCellWidget(fLog, 1, 1,1,2);
00101 
00102 
00103         QString initialText ;
00104 
00105         initialText.append(CSL1("<b>Version:</b> KPilot %1" TE_EOL)
00106                 .arg(QString::fromLatin1(KPILOT_VERSION)));
00107         initialText.append(CSL1("<b>Version:</b> pilot-link %1.%2.%3%4" TE_EOL)
00108                 .arg(PILOT_LINK_VERSION)
00109                 .arg(PILOT_LINK_MAJOR)
00110                 .arg(PILOT_LINK_MINOR)
00111 #ifdef PILOT_LINK_PATCH
00112                 .arg(QString::fromLatin1(PILOT_LINK_PATCH))
00113 #else
00114                 .arg(QString())
00115 #endif
00116                 );
00117 #ifdef KDE_VERSION_STRING
00118         initialText.append(CSL1("<b>Version:</b> KDE %1" TE_EOL)
00119                 .arg(QString::fromLatin1(KDE_VERSION_STRING)));
00120 #endif
00121 #ifdef QT_VERSION_STR
00122         initialText.append(CSL1("<b>Version:</b> Qt %1" TE_EOL)
00123                 .arg(QString::fromLatin1(QT_VERSION_STR)));
00124 #endif
00125 
00126         initialText.append(CSL1(TE_EOL));
00127         initialText.append(i18n("<qt><B>HotSync Log</B></qt>"));
00128         initialText.append(CSL1(TE_EOL));
00129 
00130 
00131         fLog->setText(initialText);
00132         fLog->scrollToBottom();
00133 
00134         QHBox *h = new QHBox(this);
00135         h->setSpacing(SPACING);
00136         QPushButton *b = new QPushButton(
00137                 i18n("Clear the text of HotSync messages","Clear Log"),
00138                 h);
00139         QWhatsThis::add(b,i18n("<qt>Clears the list of messages from the "
00140                 "current HotSync.</qt>"));
00141         connect(b,SIGNAL(clicked()),this,SLOT(clearLog()));
00142 
00143         b = new QPushButton(i18n("Save Log"),h);
00144         QWhatsThis::add(b,i18n("<qt>You can save the list of messages received "
00145                 "during this HotSync to a file (for example for use in a "
00146                 "bug report) by clicking here.</qt>"));
00147         connect(b,SIGNAL(clicked()),this,SLOT(saveLog()));
00148 
00149         fButtonBox = h;
00150 
00151         grid->addMultiCellWidget(h,2,2,1,2);
00152 
00153         fLabel = new QLabel(i18n("Sync progress:"),this);
00154         grid->addWidget(fLabel,3,1);
00155         fProgress = new KProgress(this);
00156         QWhatsThis::add(fProgress,i18n("<qt>The (estimated) percentage "
00157                 "completed in the current HotSync.</qt>"));
00158         grid->addWidget(fProgress,3,2);
00159 
00160 
00161         QString splashPath =
00162                 KGlobal::dirs()->findResource("data",
00163                         CSL1("kpilot/kpilot-splash.png"));
00164 
00165         if (!splashPath.isEmpty() && QFile::exists(splashPath))
00166         {
00167                 fLog->hide();
00168                 fLabel->hide();
00169                 fProgress->hide();
00170 
00171                 QPixmap splash(splashPath);
00172                 QPainter painter(&splash);
00173                 painter.setPen(QColor(255, 0, 0));
00174 
00175                 // This latin1() is ok; KPILOT_VERSION is a #define
00176                 // of a constant string.
00177                 int textWidth =fontMetrics().width(
00178                         QString::fromLatin1(KPILOT_VERSION)) ;
00179                 int textHeight = fontMetrics().height();
00180 
00181 #ifdef DEBUG
00182                 DEBUGKPILOT << fname
00183                         << ": Using text size "
00184                         << textWidth << "x" << textHeight
00185                         << endl;
00186 #endif
00187 
00188                 painter.fillRect(splash.width() -  28 - textWidth,
00189                         splash.height() - 6 - textHeight - textHeight ,
00190                         textWidth + 6,
00191                         textHeight + 4,
00192                         black);
00193                 painter.drawText(splash.width() -  25 - textWidth,
00194                         splash.height() - 8 - textHeight,
00195                         QString::fromLatin1(KPILOT_VERSION));
00196                 fSplash = new QLabel(this);
00197                 fSplash->setPixmap(splash);
00198                 fSplash->setAlignment(AlignCenter);
00199                 QTimer::singleShot(3000,this,SLOT(hideSplash()));
00200                 grid->addMultiCellWidget(fSplash,1,3,1,2);
00201                 grid->addColSpacing(0,10);
00202                 grid->setColStretch(1,50);
00203                 grid->setColStretch(2,50);
00204                 grid->addColSpacing(3,10);
00205         }
00206 
00207         (void) logw_id;
00208 }
00209 
00210 void LogWidget::addMessage(const QString & s)
00211 {
00212         FUNCTIONSETUP;
00213 
00214         if (s.isEmpty()) return;
00215         if (!fLog) return;
00216         QString t;
00217 
00218         if (fShowTime)
00219         {
00220                 t.append(CSL1("<b>"));
00221                 t.append(QTime::currentTime().toString());
00222                 t.append(CSL1("</b>  "));
00223         }
00224 
00225         t.append(s);
00226         
00227 #if QT_VERSION < 0x030100
00228         t.append(TE_EOL);
00229         fLog->setText(fLog->text() + t);
00230         fLog->scrollToBottom();
00231 #else
00232         fLog->append(t);
00233 #endif
00234 }
00235 
00236 void LogWidget::addError(const QString & s)
00237 {
00238         FUNCTIONSETUP;
00239 
00240         if (s.isEmpty()) return;
00241         
00242         kdWarning() << "KPilot error: " << s << endl;
00243 
00244         if (!fLog) return;
00245 
00246         QString t;
00247 
00248         t.append(CSL1("<i>"));
00249         t.append(s);
00250         t.append(CSL1("</i>"));
00251 
00252         addMessage(t);
00253 }
00254 
00255 void LogWidget::addProgress(const QString &s,int i)
00256 {
00257         FUNCTIONSETUP;
00258 
00259         if (!s.isEmpty()) logMessage(s);
00260 
00261         if ((i >= 0) && (i <= 100))
00262         {
00263                 // setValue seems to be in both KDE2 and
00264                 // KDE3, but is marked deprecated in KDE3.
00265                 //
00266                 //
00267 #ifdef KDE2
00268                 fProgress->setValue(i);
00269 #else
00270                 fProgress->setProgress(i);
00271 #endif
00272         }
00273 }
00274 
00275 void LogWidget::syncDone()
00276 {
00277         FUNCTIONSETUP;
00278 
00279         addMessage(i18n("<b>HotSync Finished!</b>"));
00280 }
00281 
00282 void LogWidget::initialize()
00283 {
00284         FUNCTIONSETUP;
00285 }
00286 
00287 void LogWidget::hideSplash()
00288 {
00289         FUNCTIONSETUP;
00290 
00291         if (fSplash)
00292         {
00293                 fSplash->hide();
00294                 KPILOT_DELETE(fSplash);
00295         }
00296 
00297         fLog->show();
00298         fLabel->show();
00299         fProgress->show();
00300 }
00301 
00302 
00303 /* DCOP */ ASYNC LogWidget::logMessage(QString s)
00304 {
00305         addMessage(s);
00306 }
00307 
00308 /* DCOP */ ASYNC LogWidget::logError(QString s)
00309 {
00310         addError(s);
00311 }
00312 
00313 /* DCOP */ ASYNC LogWidget::logProgress(QString s, int i)
00314 {
00315         addProgress(s,i);
00316 }
00317 
00318 /* slot */ void LogWidget::clearLog()
00319 {
00320         FUNCTIONSETUP;
00321 
00322         if (fLog)
00323         {
00324                 fLog->setText(QString::null);
00325         }
00326 }
00327 
00328 /* slot */ void LogWidget::saveLog()
00329 {
00330         FUNCTIONSETUP;
00331 
00332         bool finished = false;
00333 
00334         while (!finished)
00335         {
00336                 QString saveFileName = KFileDialog::getSaveFileName(
00337                         QString::null,     /* default */
00338                         CSL1("*.log"),     /* show log files by default */
00339                         this,
00340                         i18n("Save Log"));
00341 
00342                 if (saveFileName.isEmpty()) return;
00343                 if (QFile::exists(saveFileName))
00344                 {
00345                         int r = KMessageBox::warningYesNoCancel(
00346                                 this,
00347                                 i18n("The file exists. Do you want to "
00348                                         "overwrite it?"),
00349                                 i18n("File Exists"));
00350                         if (r==KMessageBox::Yes)
00351                         {
00352                                 finished=saveFile(saveFileName);
00353                         }
00354 
00355                         if (r==KMessageBox::Cancel) return;
00356                 }
00357                 else
00358                 {
00359                         finished=saveFile(saveFileName);
00360                 }
00361         }
00362 }
00363 
00364 
00365 bool LogWidget::saveFile(const QString &saveFileName)
00366 {
00367         FUNCTIONSETUP;
00368 
00369         QFile f(saveFileName);
00370         if (!f.open(IO_WriteOnly))
00371         {
00372                 int r = KMessageBox::questionYesNo(this,
00373                         i18n("<qt>Can't open the file &quot;%1&quot; "
00374                                 "for writing. Try again?</qt>"),
00375                         i18n("Can't Save"));
00376 
00377                 if (r==KMessageBox::Yes) return false;
00378                 return true;
00379         }
00380         else
00381         {
00382                 QTextStream t(&f);
00383                 t << fLog->text();
00384         }
00385 
00386         f.close();
00387         return true;
00388 }
00389 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:44 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001