00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 static const char *logw_id =
00030 "$Id: logWidget.cc,v 1.39 2003/11/18 23:57:49 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 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
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
00133 fLog->setText(initialText);
00134 fLog->scrollToBottom();
00135
00136 QHBox *h = new QHBox(this);
00137 h->setSpacing(SPACING);
00138 QPushButton *b = new QPushButton(
00139 i18n("Clear the text of HotSync messages","Clear Log"),
00140 h);
00141 QWhatsThis::add(b,i18n("<qt>Clears the list of messages from the "
00142 "current HotSync.</qt>"));
00143 connect(b,SIGNAL(clicked()),this,SLOT(clearLog()));
00144
00145 b = new QPushButton(i18n("Save Log..."),h);
00146 QWhatsThis::add(b,i18n("<qt>You can save the list of messages received "
00147 "during this HotSync to a file (for example for use in a "
00148 "bug report) by clicking here.</qt>"));
00149 connect(b,SIGNAL(clicked()),this,SLOT(saveLog()));
00150
00151 fButtonBox = h;
00152
00153 grid->addMultiCellWidget(h,2,2,1,2);
00154
00155 fLabel = new QLabel(i18n("Sync progress:"),this);
00156 grid->addWidget(fLabel,3,1);
00157 fProgress = new KProgress(this);
00158 QWhatsThis::add(fProgress,i18n("<qt>The (estimated) percentage "
00159 "completed in the current HotSync.</qt>"));
00160 grid->addWidget(fProgress,3,2);
00161
00162
00163 QString splashPath =
00164 KGlobal::dirs()->findResource("data",
00165 CSL1("kpilot/kpilot-splash.png"));
00166
00167 if (!splashPath.isEmpty() && QFile::exists(splashPath))
00168 {
00169 fLog->hide();
00170 fLabel->hide();
00171 fProgress->hide();
00172
00173 QPixmap splash(splashPath);
00174 QPainter painter(&splash);
00175 painter.setPen(QColor(255, 0, 0));
00176
00177
00178
00179 int textWidth =fontMetrics().width(
00180 QString::fromLatin1(KPILOT_VERSION)) ;
00181 int textHeight = fontMetrics().height();
00182
00183 #ifdef DEBUG
00184 DEBUGKPILOT << fname
00185 << ": Using text size "
00186 << textWidth << "x" << textHeight
00187 << endl;
00188 #endif
00189
00190 painter.fillRect(splash.width() - 28 - textWidth,
00191 splash.height() - 6 - textHeight - textHeight ,
00192 textWidth + 6,
00193 textHeight + 4,
00194 black);
00195 painter.drawText(splash.width() - 25 - textWidth,
00196 splash.height() - 8 - textHeight,
00197 QString::fromLatin1(KPILOT_VERSION));
00198 fSplash = new QLabel(this);
00199 fSplash->setPixmap(splash);
00200 fSplash->setAlignment(AlignCenter);
00201 QTimer::singleShot(3000,this,SLOT(hideSplash()));
00202 grid->addMultiCellWidget(fSplash,1,3,1,2);
00203 grid->addColSpacing(0,10);
00204 grid->setColStretch(1,50);
00205 grid->setColStretch(2,50);
00206 grid->addColSpacing(3,10);
00207 }
00208
00209 (void) logw_id;
00210 }
00211
00212 void LogWidget::addMessage(const QString & s)
00213 {
00214 FUNCTIONSETUP;
00215
00216 if (s.isEmpty()) return;
00217 if (!fLog) return;
00218 QString t;
00219
00220 if (fShowTime)
00221 {
00222 t.append(CSL1("<b>"));
00223 t.append(QTime::currentTime().toString());
00224 t.append(CSL1("</b> "));
00225 }
00226
00227 t.append(s);
00228
00229 #if QT_VERSION < 0x030100
00230 t.append(TE_EOL);
00231 fLog->setText(fLog->text() + t);
00232 #else
00233 fLog->append(t);
00234 #endif
00235 fLog->scrollToBottom();
00236 }
00237
00238 void LogWidget::addError(const QString & s)
00239 {
00240 FUNCTIONSETUP;
00241
00242 if (s.isEmpty()) return;
00243
00244 kdWarning() << "KPilot error: " << s << endl;
00245
00246 if (!fLog) return;
00247
00248 QString t;
00249
00250 t.append(CSL1("<i>"));
00251 t.append(s);
00252 t.append(CSL1("</i>"));
00253
00254 addMessage(t);
00255 }
00256
00257 void LogWidget::addProgress(const QString &s,int i)
00258 {
00259 FUNCTIONSETUP;
00260
00261 if (!s.isEmpty()) logMessage(s);
00262
00263 if ((i >= 0) && (i <= 100))
00264 {
00265
00266
00267
00268
00269 #ifdef KDE2
00270 fProgress->setValue(i);
00271 #else
00272 fProgress->setProgress(i);
00273 #endif
00274 }
00275 }
00276
00277 void LogWidget::syncDone()
00278 {
00279 FUNCTIONSETUP;
00280
00281 addMessage(i18n("<b>HotSync Finished!</b>"));
00282 }
00283
00284 void LogWidget::hideSplash()
00285 {
00286 FUNCTIONSETUP;
00287
00288 if (fSplash)
00289 {
00290 fSplash->hide();
00291 KPILOT_DELETE(fSplash);
00292 }
00293
00294 fLog->show();
00295 fLabel->show();
00296 fProgress->show();
00297 }
00298
00299
00300 ASYNC LogWidget::logMessage(QString s)
00301 {
00302 addMessage(s);
00303 }
00304
00305 ASYNC LogWidget::logError(QString s)
00306 {
00307 addError(s);
00308 }
00309
00310 ASYNC LogWidget::logProgress(QString s, int i)
00311 {
00312 addProgress(s,i);
00313 }
00314
00315 void LogWidget::clearLog()
00316 {
00317 FUNCTIONSETUP;
00318
00319 if (fLog)
00320 {
00321 fLog->setText(QString::null);
00322 }
00323 }
00324
00325 void LogWidget::saveLog()
00326 {
00327 FUNCTIONSETUP;
00328
00329 bool finished = false;
00330
00331 while (!finished)
00332 {
00333 QString saveFileName = KFileDialog::getSaveFileName(
00334 QString::null,
00335 CSL1("*.log"),
00336 this,
00337 i18n("Save Log"));
00338
00339 if (saveFileName.isEmpty()) return;
00340 if (QFile::exists(saveFileName))
00341 {
00342 int r = KMessageBox::warningYesNoCancel(
00343 this,
00344 i18n("The file exists. Do you want to "
00345 "overwrite it?"),
00346 i18n("File Exists"));
00347 if (r==KMessageBox::Yes)
00348 {
00349 finished=saveFile(saveFileName);
00350 }
00351
00352 if (r==KMessageBox::Cancel) return;
00353 }
00354 else
00355 {
00356 finished=saveFile(saveFileName);
00357 }
00358 }
00359 }
00360
00361
00362 bool LogWidget::saveFile(const QString &saveFileName)
00363 {
00364 FUNCTIONSETUP;
00365
00366 QFile f(saveFileName);
00367 if (!f.open(IO_WriteOnly))
00368 {
00369 int r = KMessageBox::questionYesNo(this,
00370 i18n("<qt>Can't open the file "%1" "
00371 "for writing. Try again?</qt>"),
00372 i18n("Can't Save"));
00373
00374 if (r==KMessageBox::Yes) return false;
00375 return true;
00376 }
00377 else
00378 {
00379 QTextStream t(&f);
00380 t << fLog->text();
00381 }
00382
00383 f.close();
00384 return true;
00385 }
00386