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.4.1 2004/04/03 14:14: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 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 initialText.append(CSL1(TE_EOL "<QT><B>KPilot has been reported to cause "
00133 "data loss. Please check with kdepim-users@kde.org.</B></QT>" TE_EOL));
00134
00135 fLog->setText(initialText);
00136 fLog->scrollToBottom();
00137
00138 QHBox *h = new QHBox(this);
00139 h->setSpacing(SPACING);
00140 QPushButton *b = new QPushButton(
00141 i18n("Clear the text of HotSync messages","Clear Log"),
00142 h);
00143 QWhatsThis::add(b,i18n("<qt>Clears the list of messages from the "
00144 "current HotSync.</qt>"));
00145 connect(b,SIGNAL(clicked()),this,SLOT(clearLog()));
00146
00147 b = new QPushButton(i18n("Save Log..."),h);
00148 QWhatsThis::add(b,i18n("<qt>You can save the list of messages received "
00149 "during this HotSync to a file (for example for use in a "
00150 "bug report) by clicking here.</qt>"));
00151 connect(b,SIGNAL(clicked()),this,SLOT(saveLog()));
00152
00153 fButtonBox = h;
00154
00155 grid->addMultiCellWidget(h,2,2,1,2);
00156
00157 fLabel = new QLabel(i18n("Sync progress:"),this);
00158 grid->addWidget(fLabel,3,1);
00159 fProgress = new KProgress(this);
00160 QWhatsThis::add(fProgress,i18n("<qt>The (estimated) percentage "
00161 "completed in the current HotSync.</qt>"));
00162 grid->addWidget(fProgress,3,2);
00163
00164
00165 QString splashPath =
00166 KGlobal::dirs()->findResource("data",
00167 CSL1("kpilot/kpilot-splash.png"));
00168
00169 if (!splashPath.isEmpty() && QFile::exists(splashPath))
00170 {
00171 fLog->hide();
00172 fLabel->hide();
00173 fProgress->hide();
00174
00175 QPixmap splash(splashPath);
00176 QPainter painter(&splash);
00177 painter.setPen(QColor(255, 0, 0));
00178
00179
00180
00181 int textWidth =fontMetrics().width(
00182 QString::fromLatin1(KPILOT_VERSION)) ;
00183 int textHeight = fontMetrics().height();
00184
00185 #ifdef DEBUG
00186 DEBUGKPILOT << fname
00187 << ": Using text size "
00188 << textWidth << "x" << textHeight
00189 << endl;
00190 #endif
00191
00192 painter.fillRect(splash.width() - 28 - textWidth,
00193 splash.height() - 6 - textHeight - textHeight ,
00194 textWidth + 6,
00195 textHeight + 4,
00196 black);
00197 painter.drawText(splash.width() - 25 - textWidth,
00198 splash.height() - 8 - textHeight,
00199 QString::fromLatin1(KPILOT_VERSION));
00200 fSplash = new QLabel(this);
00201 fSplash->setPixmap(splash);
00202 fSplash->setAlignment(AlignCenter);
00203 QTimer::singleShot(3000,this,SLOT(hideSplash()));
00204 grid->addMultiCellWidget(fSplash,1,3,1,2);
00205 grid->addColSpacing(0,10);
00206 grid->setColStretch(1,50);
00207 grid->setColStretch(2,50);
00208 grid->addColSpacing(3,10);
00209 }
00210
00211 (void) logw_id;
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 kdWarning() << "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
00268
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 ASYNC LogWidget::logMessage(QString s)
00303 {
00304 addMessage(s);
00305 }
00306
00307 ASYNC LogWidget::logError(QString s)
00308 {
00309 addError(s);
00310 }
00311
00312 ASYNC LogWidget::logProgress(QString s, int i)
00313 {
00314 addProgress(s,i);
00315 }
00316
00317 void LogWidget::clearLog()
00318 {
00319 FUNCTIONSETUP;
00320
00321 if (fLog)
00322 {
00323 fLog->setText(QString::null);
00324 }
00325 }
00326
00327 void LogWidget::saveLog()
00328 {
00329 FUNCTIONSETUP;
00330
00331 bool finished = false;
00332
00333 while (!finished)
00334 {
00335 QString saveFileName = KFileDialog::getSaveFileName(
00336 QString::null,
00337 CSL1("*.log"),
00338 this,
00339 i18n("Save Log"));
00340
00341 if (saveFileName.isEmpty()) return;
00342 if (QFile::exists(saveFileName))
00343 {
00344 int r = KMessageBox::warningYesNoCancel(
00345 this,
00346 i18n("The file exists. Do you want to "
00347 "overwrite it?"),
00348 i18n("File Exists"));
00349 if (r==KMessageBox::Yes)
00350 {
00351 finished=saveFile(saveFileName);
00352 }
00353
00354 if (r==KMessageBox::Cancel) return;
00355 }
00356 else
00357 {
00358 finished=saveFile(saveFileName);
00359 }
00360 }
00361 }
00362
00363
00364 bool LogWidget::saveFile(const QString &saveFileName)
00365 {
00366 FUNCTIONSETUP;
00367
00368 QFile f(saveFileName);
00369 if (!f.open(IO_WriteOnly))
00370 {
00371 int r = KMessageBox::questionYesNo(this,
00372 i18n("<qt>Can't open the file "%1" "
00373 "for writing. Try again?</qt>"),
00374 i18n("Can't Save"));
00375
00376 if (r==KMessageBox::Yes) return false;
00377 return true;
00378 }
00379 else
00380 {
00381 QTextStream t(&f);
00382 t << fLog->text();
00383 }
00384
00385 f.close();
00386 return true;
00387 }
00388