kpilot/kpilot

logWidget.cc

00001 /* 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., 51 Franklin Street, Fifth Floor, Boston,
00023 ** MA 02110-1301, 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 438196 2005-07-24 15:42:44Z binner $";
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     DCOPObject("LogIface"),
00070     PilotComponent(parent, "component_log", QString::null),
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     fLog->setWordWrap(QTextEdit::WidgetWidth);
00093     fLog->setWrapPolicy(QTextEdit::AtWordOrDocumentBoundary);
00094 #if QT_VERSION < 0x030100
00095     /* nothing, use AutoText */
00096 #else
00097     fLog->setTextFormat(Qt::LogText);
00098 #endif
00099 
00100     QWhatsThis::add(fLog, i18n("<qt>This lists all the messages received "
00101             "during the current HotSync</qt>"));
00102     grid->addMultiCellWidget(fLog, 1, 1,1,2);
00103 
00104 
00105     QString initialText ;
00106 
00107     initialText.append(CSL1("<b>Version:</b> KPilot %1" TE_EOL)
00108         .arg(QString::fromLatin1(KPILOT_VERSION)));
00109     initialText.append(CSL1("<b>Version:</b> pilot-link %1.%2.%3%4" TE_EOL)
00110         .arg(PILOT_LINK_VERSION)
00111         .arg(PILOT_LINK_MAJOR)
00112         .arg(PILOT_LINK_MINOR)
00113 #ifdef PILOT_LINK_PATCH
00114         .arg(QString::fromLatin1(PILOT_LINK_PATCH))
00115 #else
00116         .arg(QString())
00117 #endif
00118         );
00119 #ifdef KDE_VERSION_STRING
00120     initialText.append(CSL1("<b>Version:</b> KDE %1" TE_EOL)
00121         .arg(QString::fromLatin1(KDE_VERSION_STRING)));
00122 #endif
00123 #ifdef QT_VERSION_STR
00124     initialText.append(CSL1("<b>Version:</b> Qt %1" TE_EOL)
00125         .arg(QString::fromLatin1(QT_VERSION_STR)));
00126 #endif
00127 
00128     initialText.append(CSL1(TE_EOL));
00129     initialText.append(i18n("<qt><b>HotSync Log</b></qt>"));
00130     initialText.append(CSL1(TE_EOL));
00131 
00132 #if KDE_IS_VERSION(3,3,0)
00133 #else
00134     initialText.append(CSL1(TE_EOL "<qt><b>KDE 3.2 is no longer supported. Please update to KDE 3.3 or later.</b></qt>" TE_EOL));
00135     initialText.append(CSL1(TE_EOL "<qt><b>You may be unable to do conflict resolution.</b></qt>" TE_EOL));
00136 #endif
00137 
00138     fLog->setText(initialText);
00139     fLog->scrollToBottom();
00140 
00141     QHBox *h = new QHBox(this);
00142     h->setSpacing(SPACING);
00143     QPushButton *b = new QPushButton(
00144         i18n("Clear the text of HotSync messages","Clear Log"),
00145         h);
00146     QWhatsThis::add(b,i18n("<qt>Clears the list of messages from the "
00147         "current HotSync.</qt>"));
00148     connect(b,SIGNAL(clicked()),this,SLOT(clearLog()));
00149 
00150     b = new QPushButton(i18n("Save Log..."),h);
00151     QWhatsThis::add(b,i18n("<qt>You can save the list of messages received "
00152         "during this HotSync to a file (for example for use in a "
00153         "bug report) by clicking here.</qt>"));
00154     connect(b,SIGNAL(clicked()),this,SLOT(saveLog()));
00155 
00156     fButtonBox = h;
00157 
00158     grid->addMultiCellWidget(h,2,2,1,2);
00159 
00160     fLabel = new QLabel(i18n("Sync progress:"),this);
00161     grid->addWidget(fLabel,3,1);
00162     fProgress = new KProgress(this);
00163     QWhatsThis::add(fProgress,i18n("<qt>The (estimated) percentage "
00164         "completed in the current HotSync.</qt>"));
00165     grid->addWidget(fProgress,3,2);
00166 
00167 
00168     QString splashPath =
00169         KGlobal::dirs()->findResource("data",
00170             CSL1("kpilot/kpilot-splash.png"));
00171 
00172     if (!splashPath.isEmpty() && QFile::exists(splashPath))
00173     {
00174         fLog->hide();
00175         fLabel->hide();
00176         fProgress->hide();
00177 
00178         QPixmap splash(splashPath);
00179         QPainter painter(&splash);
00180         painter.setPen(QColor(0, 255, 0));
00181 
00182         // This latin1() is ok; KPILOT_VERSION is a #define
00183         // of a constant string.
00184         int textWidth =fontMetrics().width(
00185             QString::fromLatin1(KPILOT_VERSION)) ;
00186         int textHeight = fontMetrics().height();
00187 
00188 #ifdef DEBUG
00189         DEBUGKPILOT << fname
00190             << ": Using text size "
00191             << textWidth << "x" << textHeight
00192             << endl;
00193 #endif
00194 
00195         painter.fillRect(splash.width() -  28 - textWidth,
00196             splash.height() - 6 - textHeight,
00197             textWidth + 6,
00198             textHeight + 4,
00199             black);
00200         painter.drawText(splash.width() -  25 - textWidth,
00201             splash.height() - 8,
00202             QString::fromLatin1(KPILOT_VERSION));
00203         fSplash = new QLabel(this);
00204         fSplash->setPixmap(splash);
00205         fSplash->setAlignment(AlignCenter);
00206         QTimer::singleShot(3000,this,SLOT(hideSplash()));
00207         grid->addMultiCellWidget(fSplash,1,3,1,2);
00208         grid->addColSpacing(0,10);
00209         grid->setColStretch(1,50);
00210         grid->setColStretch(2,50);
00211         grid->addColSpacing(3,10);
00212     }
00213 
00214     (void) logw_id;
00215 }
00216 
00217 void LogWidget::addMessage(const QString & s)
00218 {
00219     FUNCTIONSETUP;
00220 
00221     if (s.isEmpty()) return;
00222     if (!fLog) return;
00223     QString t;
00224 
00225     if (fShowTime)
00226     {
00227         t.append(CSL1("<b>"));
00228         t.append(QTime::currentTime().toString());
00229         t.append(CSL1("</b> "));
00230     }
00231 
00232     t.append(s);
00233 
00234 #if QT_VERSION < 0x030100
00235     t.append(TE_EOL);
00236     fLog->setText(fLog->text() + t);
00237 #else
00238     fLog->append(t);
00239 #endif
00240     fLog->scrollToBottom();
00241 }
00242 
00243 void LogWidget::addError(const QString & s)
00244 {
00245     FUNCTIONSETUP;
00246 
00247     if (s.isEmpty()) return;
00248 
00249     kdWarning() << "KPilot error: " << s << endl;
00250 
00251     if (!fLog) return;
00252 
00253     QString t;
00254 
00255     t.append(CSL1("<i>"));
00256     t.append(s);
00257     t.append(CSL1("</i>"));
00258 
00259     addMessage(t);
00260 }
00261 
00262 void LogWidget::addProgress(const QString &s,int i)
00263 {
00264     FUNCTIONSETUP;
00265 
00266     if (!s.isEmpty()) logMessage(s);
00267 
00268     if ((i >= 0) && (i <= 100))
00269     {
00270         // setValue seems to be in both KDE2 and
00271         // KDE3, but is marked deprecated in KDE3.
00272         //
00273         //
00274 #ifdef KDE2
00275         fProgress->setValue(i);
00276 #else
00277         fProgress->setProgress(i);
00278 #endif
00279     }
00280 }
00281 
00282 void LogWidget::syncDone()
00283 {
00284     FUNCTIONSETUP;
00285 
00286     addMessage(i18n("<b>HotSync Finished.</b>"));
00287 }
00288 
00289 void LogWidget::hideSplash()
00290 {
00291     FUNCTIONSETUP;
00292 
00293     if (fSplash)
00294     {
00295         fSplash->hide();
00296         KPILOT_DELETE(fSplash);
00297     }
00298 
00299     fLog->show();
00300     fLabel->show();
00301     fProgress->show();
00302 }
00303 
00304 
00305 /* DCOP */ ASYNC LogWidget::logMessage(QString s)
00306 {
00307     addMessage(s);
00308 }
00309 
00310 /* DCOP */ ASYNC LogWidget::logError(QString s)
00311 {
00312     addError(s);
00313 }
00314 
00315 /* DCOP */ ASYNC LogWidget::logProgress(QString s, int i)
00316 {
00317     addProgress(s,i);
00318 }
00319 
00320 /* DCOP */ ASYNC LogWidget::logStartSync()
00321 {
00322 }
00323 
00324 /* DCOP */ ASYNC LogWidget::logEndSync()
00325 {
00326 }
00327 
00328 /* slot */ void LogWidget::clearLog()
00329 {
00330     FUNCTIONSETUP;
00331 
00332     if (fLog)
00333     {
00334         fLog->setText(QString::null);
00335     }
00336 }
00337 
00338 /* slot */ void LogWidget::saveLog()
00339 {
00340     FUNCTIONSETUP;
00341 
00342     bool finished = false;
00343 
00344     while (!finished)
00345     {
00346         QString saveFileName = KFileDialog::getSaveFileName(
00347             QString::null,     /* default */
00348             CSL1("*.log"),     /* show log files by default */
00349             this,
00350             i18n("Save Log"));
00351 
00352         if (saveFileName.isEmpty()) return;
00353         if (QFile::exists(saveFileName))
00354         {
00355             int r = KMessageBox::warningYesNoCancel(
00356                 this,
00357                 i18n("The file exists. Do you want to "
00358                     "overwrite it?"),
00359                 i18n("File Exists"), i18n("Overwrite"), i18n("Do Not Overwrite"));
00360             if (r==KMessageBox::Yes)
00361             {
00362                 finished=saveFile(saveFileName);
00363             }
00364 
00365             if (r==KMessageBox::Cancel) return;
00366         }
00367         else
00368         {
00369             finished=saveFile(saveFileName);
00370         }
00371     }
00372 }
00373 
00374 
00375 bool LogWidget::saveFile(const QString &saveFileName)
00376 {
00377     FUNCTIONSETUP;
00378 
00379     QFile f(saveFileName);
00380     if (!f.open(IO_WriteOnly))
00381     {
00382         int r = KMessageBox::questionYesNo(this,
00383             i18n("<qt>Cannot open the file &quot;%1&quot; "
00384                 "for writing; try again?</qt>"),
00385             i18n("Cannot Save"), i18n("Try Again"), i18n("Do Not Try"));
00386 
00387         if (r==KMessageBox::Yes) return false;
00388         return true;
00389     }
00390     else
00391     {
00392         QTextStream t(&f);
00393         t << fLog->text();
00394     }
00395 
00396     f.close();
00397     return true;
00398 }
00399 
KDE Home | KDE Accessibility Home | Description of Access Keys