Handle language change events in widgets.
[maemo-rb.git] / rbutil / rbutilqt / systrace.cpp
blob20105e653839a11f2c8db6c9d667ef2264f26578
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Riebeling
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ****************************************************************************/
19 #include <QtGui>
20 #include "systrace.h"
21 #include "ui_systracefrm.h"
23 #include "rbsettings.h"
25 QString SysTrace::debugbuffer;
26 QString SysTrace::lastmessage;
27 unsigned int SysTrace::repeat = 0;
29 SysTrace::SysTrace(QWidget *parent) : QDialog(parent)
31 ui.setupUi(this);
32 ui.textTrace->setReadOnly(true);
33 ui.textTrace->setLayoutDirection(Qt::LeftToRight);
34 refresh();
36 connect(ui.buttonClose, SIGNAL(clicked()), this, SLOT(close()));
37 connect(ui.buttonSave, SIGNAL(clicked()), this, SLOT(saveCurrentTrace()));
38 connect(ui.buttonSavePrevious, SIGNAL(clicked()), this, SLOT(savePreviousTrace()));
39 connect(ui.buttonRefresh, SIGNAL(clicked()), this, SLOT(refresh()));
42 void SysTrace::refresh(void)
44 int pos = ui.textTrace->verticalScrollBar()->value();
45 flush();
46 ui.textTrace->setHtml("<pre>" + debugbuffer + "</pre>");
47 ui.textTrace->verticalScrollBar()->setValue(pos);
48 QString oldlog = RbSettings::value(RbSettings::CachePath).toString()
49 + "/rbutil-trace.log";
50 ui.buttonSavePrevious->setEnabled(QFileInfo(oldlog).isFile());
54 void SysTrace::save(QString filename)
56 if(filename.isEmpty())
57 filename = RbSettings::value(RbSettings::CachePath).toString()
58 + "/rbutil-trace.log";
59 // make sure any repeat detection is flushed
60 flush();
61 // append save date to the trace. Append it directly instead of using
62 // qDebug() as the handler might have been unregistered.
63 debugbuffer.append("[SysTrace] saving trace at ");
64 debugbuffer.append(QDateTime::currentDateTime().toString(Qt::ISODate));
65 debugbuffer.append("\n");
66 QFile fh(filename);
67 if(!fh.open(QIODevice::WriteOnly))
68 return;
69 fh.write(debugbuffer.toUtf8(), debugbuffer.size());
70 fh.close();
73 void SysTrace::saveCurrentTrace(void)
75 QString fp = QFileDialog::getSaveFileName(this, tr("Save system trace log"),
76 QDir::homePath(), "*.log");
77 if(!fp.isEmpty())
78 save(fp);
82 void SysTrace::savePreviousTrace(void)
84 QString fp = QFileDialog::getSaveFileName(this, tr("Save system trace log"),
85 QDir::homePath(), "*.log");
86 if(fp.isEmpty())
87 return;
89 QString oldlog = RbSettings::value(RbSettings::CachePath).toString()
90 + "/rbutil-trace.log";
91 QFile::copy(oldlog, fp);
92 return;
95 void SysTrace::debug(QtMsgType type, const char* msg)
97 (void)type;
98 if(lastmessage != msg) {
99 lastmessage = msg;
100 flush();
101 debugbuffer.append(QString::fromLocal8Bit(msg) + "\n");
102 #if !defined(NODEBUG)
103 fprintf(stderr, "%s\n", msg);
104 #endif
105 repeat = 1;
107 else {
108 repeat++;
112 void SysTrace::flush(void)
114 if(repeat > 1) {
115 debugbuffer.append(
116 QString(" (Last message repeated %1 times.)\n").arg(repeat));
117 #if !defined(NODEBUG)
118 fprintf(stderr, " (Last message repeated %i times.)\n", repeat);
119 #endif
124 void SysTrace::changeEvent(QEvent *e)
126 if(e->type() == QEvent::LanguageChange) {
127 ui.retranslateUi(this);
128 } else {
129 QWidget::changeEvent(e);