1
2 #include "logviewer.h"
3 #include "logger.h"
4
5 #include <QLayout>
6 #include <QLabel>
7 #include <QApplication>
8
9 #include <KLocale>
10 #include <KIcon>
11 #include <KPushButton>
12 #include <KComboBox>
13 #include <KTextEdit>
14 #include <KFileDialog>
15 #include <KMessageBox>
16
17 #include <KGlobal>
18
19
LogViewer(Logger * _logger,QWidget * parent,Qt::WFlags f)20 LogViewer::LogViewer( Logger* _logger, QWidget *parent, Qt::WFlags f )
21 : KDialog( parent, f ),
22 logger( _logger )
23 {
24 const int fontHeight = QFontMetrics(QApplication::font()).boundingRect("M").size().height();
25
26 connect( logger, SIGNAL(removedProcess(int)), this, SLOT(processRemoved(int)) );
27 connect( logger, SIGNAL(updateProcess(int)), this, SLOT(updateProcess(int)) );
28
29 setCaption( i18n("Log Viewer") );
30 setWindowIcon( KIcon("view-list-text") );
31 setButtons( KDialog::User1 | KDialog::User2 | KDialog::Close );
32 setButtonText( KDialog::User1, i18n("Update") );
33 setButtonIcon( KDialog::User1, KIcon("view-refresh") );
34 connect( this, SIGNAL(user1Clicked()), this, SLOT(refillLogs()) );
35 setButtonText( KDialog::User2, i18n("Save to file...") );
36 setButtonIcon( KDialog::User2, KIcon("document-save") );
37 connect( this, SIGNAL(user2Clicked()), this, SLOT(save()) );
38 setButtonFocus( KDialog::Close );
39
40 QWidget *widget = new QWidget( this );
41 setMainWidget( widget );
42 QVBoxLayout *box = new QVBoxLayout( widget );
43
44 QHBoxLayout *topBox = new QHBoxLayout( widget );
45 box->addLayout( topBox );
46 QLabel *lItem = new QLabel( i18n("Log file:") );
47 topBox->addWidget( lItem );
48 topBox->setStretchFactor( lItem, 0 );
49 cItem = new KComboBox( this );
50 topBox->addWidget( cItem );
51 topBox->setStretchFactor( cItem, 1 );
52 connect( cItem, SIGNAL(activated(int)), this, SLOT(itemChanged()) );
53
54 kLog = new KTextEdit( this );
55 kLog->setTabStopWidth( kLog->tabStopWidth()/2 );
56 box->addWidget( kLog );
57 kLog->setTextInteractionFlags( Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard );
58
59 refillLogs();
60
61 setInitialSize( QSize(60*fontHeight,40*fontHeight) );
62 KSharedConfig::Ptr conf = KGlobal::config();
63 KConfigGroup group = conf->group( "LogViewer" );
64 restoreDialogSize( group );
65 }
66
~LogViewer()67 LogViewer::~LogViewer()
68 {
69 KSharedConfig::Ptr conf = KGlobal::config();
70 KConfigGroup group = conf->group( "LogViewer" );
71 saveDialogSize( group );
72 }
73
refillLogs()74 void LogViewer::refillLogs()
75 {
76 const int currentProcess = cItem->itemData(cItem->currentIndex()).toInt();
77
78 cItem->clear();
79
80 QPair<int, QString> log;
81 foreach( log, logger->getLogs() )
82 {
83 const int id = log.first;
84 QString name = log.second;
85 // TODO make the string width dependend on the window width
86 if( name.length() > 73 )
87 name = name.left(35) + "..." + name.right(35);
88
89 if( id == 1000 )
90 cItem->addItem( i18n("soundKonverter application log"), QVariant(id) );
91 else
92 cItem->addItem( name, QVariant(id) );
93 }
94
95 if( cItem->findData(currentProcess) != -1 )
96 cItem->setCurrentIndex( cItem->findData(currentProcess) );
97 else
98 cItem->setCurrentIndex( 0 );
99
100 itemChanged();
101 }
102
itemChanged()103 void LogViewer::itemChanged()
104 {
105 // HACK avoid Qt bug? changing the color of 'uncolored' text when switching the log file
106 QTextCursor cursor = kLog->textCursor();
107 cursor.setPosition( 0 );
108 kLog->setTextCursor( cursor );
109
110 kLog->clear();
111 const LoggerItem* const item = logger->getLog( cItem->itemData(cItem->currentIndex()).toInt() );
112
113 if( !item )
114 return;
115
116 foreach( const QString& line, item->data )
117 kLog->append( line );
118
119 QPalette currentPalette = kLog->palette();
120 if( item->completed )
121 {
122 currentPalette.setColor( QPalette::Base, QApplication::palette().base().color() );
123 }
124 else
125 {
126 currentPalette.setColor( QPalette::Base, QColor(255,234,234) );
127 }
128 kLog->setPalette( currentPalette );
129 }
130
save()131 void LogViewer::save()
132 {
133 const QString fileName = KFileDialog::getSaveFileName( KUrl(), "*.txt\n*.log", this, i18n("Save log file") );
134 if( fileName.isEmpty() )
135 return;
136
137 QFile file( fileName );
138 if( file.exists() )
139 {
140 if( KMessageBox::questionYesNo(this,i18n("File already exists. Do you really want to overwrite it?")) == KMessageBox::No )
141 return;
142 }
143 if( !file.open(QIODevice::WriteOnly) )
144 {
145 KMessageBox::error( this, i18n("Writing to file failed.\nMaybe you haven't got write permission.") );
146 return;
147 }
148 QTextStream textStream;
149 textStream.setDevice( &file );
150 textStream << kLog->toPlainText();
151 file.close();
152 }
153
processRemoved(int id)154 void LogViewer::processRemoved( int id )
155 {
156 Q_UNUSED(id)
157
158 refillLogs();
159 }
160
updateProcess(int id)161 void LogViewer::updateProcess( int id )
162 {
163 Q_UNUSED(id)
164
165 refillLogs();
166 }
167
showLog(int id)168 void LogViewer::showLog( int id )
169 {
170 if( cItem->findData(QVariant(id)) != -1 )
171 cItem->setCurrentIndex( cItem->findData(QVariant(id)) );
172 else
173 cItem->setCurrentIndex( 0 );
174
175 itemChanged();
176 }
177
178