kpilot

logWidget.cc

Go to the documentation of this file.
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 
00030 #include "options.h"
00031 
00032 #include <qfile.h>
00033 #include <qlayout.h>
00034 #include <qtextedit.h>
00035 #include <qwhatsthis.h>
00036 #include <qdatetime.h>
00037 #include <qlabel.h>
00038 #include <qpixmap.h>
00039 #include <qtimer.h>
00040 #include <qpushbutton.h>
00041 #include <qhbox.h>
00042 #include <qtextstream.h>
00043 #include <qpainter.h>
00044 
00045 #include <kglobal.h>
00046 #include <kstandarddirs.h>
00047 #include <kprogress.h>
00048 #include <kfiledialog.h>
00049 #include <kmessagebox.h>
00050 
00051 #include <pi-version.h>
00052 
00053 #ifndef PILOT_LINK_PATCH
00054 #define PILOT_LINK_PATCH "unknown"
00055 #endif
00056 
00057 #include "logWidget.moc"
00058 
00059 #if QT_VERSION < 0x030100
00060 #define TE_EOL "<br/>"
00061 #else
00062 #define TE_EOL "\n"
00063 #endif
00064 
00065 
00066 LogWidget::LogWidget(QWidget * parent) :
00067     DCOPObject("LogIface"),
00068     PilotComponent(parent, "component_log", QString::null),
00069     fLog(0L),
00070     fShowTime(false),
00071     fSplash(0L),
00072     fLabel(0L),
00073     fProgress(0L),
00074     fButtonBox(0L)
00075 {
00076     FUNCTIONSETUP;
00077     QGridLayout *grid = new QGridLayout(this, 4, 4, SPACING);
00078 
00079     grid->addRowSpacing(0, SPACING);
00080     grid->addRowSpacing(1, 100);
00081     grid->addColSpacing(2, 100);
00082     grid->addRowSpacing(3, SPACING);
00083     grid->addColSpacing(0, SPACING);
00084     grid->addColSpacing(3, SPACING);
00085     grid->setRowStretch(1, 50);
00086     grid->setColStretch(2, 50);
00087 
00088     fLog = new QTextEdit(this);
00089     fLog->setReadOnly(true);
00090     fLog->setWordWrap(QTextEdit::WidgetWidth);
00091     fLog->setWrapPolicy(QTextEdit::AtWordOrDocumentBoundary);
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 #if KDE_IS_VERSION(3,3,0)
00131 #else
00132     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));
00133     initialText.append(CSL1(TE_EOL "<qt><b>You may be unable to do conflict resolution.</b></qt>" TE_EOL));
00134 #endif
00135 
00136     fLog->setText(initialText);
00137     fLog->scrollToBottom();
00138 
00139     QHBox *h = new QHBox(this);
00140     h->setSpacing(SPACING);
00141     QPushButton *b = new QPushButton(
00142         i18n("Clear the text of HotSync messages","Clear Log"),
00143         h);
00144     QWhatsThis::add(b,i18n("<qt>Clears the list of messages from the "
00145         "current HotSync.</qt>"));
00146     connect(b,SIGNAL(clicked()),this,SLOT(clearLog()));
00147 
00148     b = new QPushButton(i18n("Save Log..."),h);
00149     QWhatsThis::add(b,i18n("<qt>You can save the list of messages received "
00150         "during this HotSync to a file (for example for use in a "
00151         "bug report) by clicking here.</qt>"));
00152     connect(b,SIGNAL(clicked()),this,SLOT(saveLog()));
00153 
00154     fButtonBox = h;
00155 
00156     grid->addMultiCellWidget(h,2,2,1,2);
00157 
00158     fLabel = new QLabel(i18n("Sync progress:"),this);
00159     grid->addWidget(fLabel,3,1);
00160     fProgress = new KProgress(this);
00161     QWhatsThis::add(fProgress,i18n("<qt>The (estimated) percentage "
00162         "completed in the current HotSync.</qt>"));
00163     grid->addWidget(fProgress,3,2);
00164 
00165 
00166     QString splashPath =
00167         KGlobal::dirs()->findResource("data",
00168             CSL1("kpilot/kpilot-splash.png"));
00169 
00170     if (!splashPath.isEmpty() && QFile::exists(splashPath))
00171     {
00172         fLog->hide();
00173         fLabel->hide();
00174         fProgress->hide();
00175 
00176         QPixmap splash(splashPath);
00177         QPainter painter(&splash);
00178         painter.setPen(QColor(0, 255, 0));
00179 
00180         // This latin1() is ok; KPILOT_VERSION is a #define
00181         // of a constant string.
00182         int textWidth =fontMetrics().width(
00183             QString::fromLatin1(KPILOT_VERSION)) ;
00184         int textHeight = fontMetrics().height();
00185 
00186 #ifdef DEBUG
00187         DEBUGKPILOT << fname
00188             << ": Using text size "
00189             << textWidth << "x" << textHeight
00190             << endl;
00191 #endif
00192 
00193         painter.fillRect(splash.width() -  28 - textWidth,
00194             splash.height() - 6 - textHeight,
00195             textWidth + 6,
00196             textHeight + 4,
00197             black);
00198         painter.drawText(splash.width() -  25 - textWidth,
00199             splash.height() - 8,
00200             QString::fromLatin1(KPILOT_VERSION));
00201         fSplash = new QLabel(this);
00202         fSplash->setPixmap(splash);
00203         fSplash->setAlignment(AlignCenter);
00204         QTimer::singleShot(3000,this,SLOT(hideSplash()));
00205         grid->addMultiCellWidget(fSplash,1,3,1,2);
00206         grid->addColSpacing(0,10);
00207         grid->setColStretch(1,50);
00208         grid->setColStretch(2,50);
00209         grid->addColSpacing(3,10);
00210     }
00211 
00212 }
00213 
00214 void LogWidget::addMessage(const QString & s)
00215 {
00216     FUNCTIONSETUP;
00217 
00218     if (s.isEmpty()) return;
00219     if (!fLog) return;
00220     QString t;
00221 
00222     if (fShowTime)
00223     {
00224         t.append(CSL1("<b>"));
00225         t.append(QTime::currentTime().toString());
00226         t.append(CSL1("</b> "));
00227     }
00228 
00229     t.append(s);
00230 
00231 #if QT_VERSION < 0x030100
00232     t.append(TE_EOL);
00233     fLog->setText(fLog->text() + t);
00234 #else
00235     fLog->append(t);
00236 #endif
00237     fLog->scrollToBottom();
00238 }
00239 
00240 void LogWidget::addError(const QString & s)
00241 {
00242     FUNCTIONSETUP;
00243 
00244     if (s.isEmpty()) return;
00245 
00246     WARNINGKPILOT << "KPilot error: " << s << endl;
00247 
00248     if (!fLog) return;
00249 
00250     QString t;
00251 
00252     t.append(CSL1("<i>"));
00253     t.append(s);
00254     t.append(CSL1("</i>"));
00255 
00256     addMessage(t);
00257 }
00258 
00259 void LogWidget::addProgress(const QString &s,int i)
00260 {
00261     FUNCTIONSETUP;
00262 
00263     if (!s.isEmpty()) logMessage(s);
00264 
00265     if ((i >= 0) && (i <= 100))
00266     {
00267         // setValue seems to be in both KDE2 and
00268         // KDE3, but is marked deprecated in KDE3.
00269         //
00270         //
00271 #ifdef KDE2
00272         fProgress->setValue(i);
00273 #else
00274         fProgress->setProgress(i);
00275 #endif
00276     }
00277 }
00278 
00279 void LogWidget::syncDone()
00280 {
00281     FUNCTIONSETUP;
00282 
00283     addMessage(i18n("<b>HotSync Finished.</b>"));
00284 }
00285 
00286 void LogWidget::hideSplash()
00287 {
00288     FUNCTIONSETUP;
00289 
00290     if (fSplash)
00291     {
00292         fSplash->hide();
00293         KPILOT_DELETE(fSplash);
00294     }
00295 
00296     fLog->show();
00297     fLabel->show();
00298     fProgress->show();
00299 }
00300 
00301 
00302 /* DCOP */ ASYNC LogWidget::logMessage(QString s)
00303 {
00304     addMessage(s);
00305 }
00306 
00307 /* DCOP */ ASYNC LogWidget::logError(QString s)
00308 {
00309     addError(s);
00310 }
00311 
00312 /* DCOP */ ASYNC LogWidget::logProgress(QString s, int i)
00313 {
00314     addProgress(s,i);
00315 }
00316 
00317 /* DCOP */ ASYNC LogWidget::logStartSync()
00318 {
00319 }
00320 
00321 /* DCOP */ ASYNC LogWidget::logEndSync()
00322 {
00323 }
00324 
00325 /* slot */ void LogWidget::clearLog()
00326 {
00327     FUNCTIONSETUP;
00328 
00329     if (fLog)
00330     {
00331         fLog->setText(QString::null);
00332     }
00333 }
00334 
00335 /* slot */ void LogWidget::saveLog()
00336 {
00337     FUNCTIONSETUP;
00338 
00339     bool finished = false;
00340 
00341     while (!finished)
00342     {
00343         QString saveFileName = KFileDialog::getSaveFileName(
00344             QString::null,     /* default */
00345             CSL1("*.log"),     /* show log files by default */
00346             this,
00347             i18n("Save Log"));
00348 
00349         if (saveFileName.isEmpty()) return;
00350         if (QFile::exists(saveFileName))
00351         {
00352             int r = KMessageBox::warningYesNoCancel(
00353                 this,
00354                 i18n("The file exists. Do you want to "
00355                     "overwrite it?"),
00356                 i18n("File Exists"), i18n("Overwrite"), i18n("Do Not Overwrite"));
00357             if (r==KMessageBox::Yes)
00358             {
00359                 finished=saveFile(saveFileName);
00360             }
00361 
00362             if (r==KMessageBox::Cancel) return;
00363         }
00364         else
00365         {
00366             finished=saveFile(saveFileName);
00367         }
00368     }
00369 }
00370 
00371 
00372 bool LogWidget::saveFile(const QString &saveFileName)
00373 {
00374     FUNCTIONSETUP;
00375 
00376     QFile f(saveFileName);
00377     if (!f.open(IO_WriteOnly))
00378     {
00379         int r = KMessageBox::questionYesNo(this,
00380             i18n("<qt>Cannot open the file &quot;%1&quot; "
00381                 "for writing; try again?</qt>"),
00382             i18n("Cannot Save"), i18n("Try Again"), i18n("Do Not Try"));
00383 
00384         if (r==KMessageBox::Yes) return false;
00385         return true;
00386     }
00387     else
00388     {
00389         QTextStream t(&f);
00390         t << fLog->text();
00391     }
00392 
00393     f.close();
00394     return true;
00395 }
00396