1 | /**
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * "Virtual Log Viewer" dialog UI include (Qt Designer)
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006 innotek GmbH
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.215389.xyz. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /****************************************************************************
|
---|
24 | ** ui.h extension file, included from the uic-generated form implementation.
|
---|
25 | **
|
---|
26 | ** If you wish to add, delete or rename functions or slots use
|
---|
27 | ** Qt Designer which will update this file, preserving your code. Create an
|
---|
28 | ** init() function in place of a constructor, and a destroy() function in
|
---|
29 | ** place of a destructor.
|
---|
30 | *****************************************************************************/
|
---|
31 |
|
---|
32 |
|
---|
33 | VBoxVMLogViewer *VBoxVMLogViewer::mSelf = 0;
|
---|
34 | void VBoxVMLogViewer::createLogViewer (CMachine &aMachine)
|
---|
35 | {
|
---|
36 | if (!mSelf)
|
---|
37 | {
|
---|
38 | /* creating new log viewer if there is no one existing */
|
---|
39 | mSelf = new VBoxVMLogViewer (0, "VBoxVMLogViewer",
|
---|
40 | WType_TopLevel | WDestructiveClose);
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (mSelf->machine() != aMachine)
|
---|
44 | {
|
---|
45 | /* re-read new machine data if the machine was changed or
|
---|
46 | * the log-viewer is opened for the first time */
|
---|
47 | mSelf->setup (aMachine);
|
---|
48 | }
|
---|
49 |
|
---|
50 | mSelf->show();
|
---|
51 | mSelf->setWindowState (mSelf->windowState() & ~WindowMinimized);
|
---|
52 | mSelf->setActiveWindow();
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | void VBoxVMLogViewer::init()
|
---|
57 | {
|
---|
58 | /* prepare dialog to first run */
|
---|
59 | mFirstRun = true;
|
---|
60 |
|
---|
61 | /* dialog initially is not polished */
|
---|
62 | mIsPolished = false;
|
---|
63 |
|
---|
64 | /* search the default button */
|
---|
65 | mDefaultButton = searchDefaultButton();
|
---|
66 | qApp->installEventFilter (this);
|
---|
67 |
|
---|
68 | /* setup a dialog icon */
|
---|
69 | setIcon (QPixmap::fromMimeSource ("show_logs_16px.png"));
|
---|
70 |
|
---|
71 | /* statusbar initially disabled */
|
---|
72 | statusBar()->setHidden (true);
|
---|
73 |
|
---|
74 | /* setup size grip */
|
---|
75 | mSizeGrip = new QSizeGrip (centralWidget(), "mSizeGrip");
|
---|
76 | mSizeGrip->resize (mSizeGrip->sizeHint());
|
---|
77 | mSizeGrip->stackUnder (mCloseButton);
|
---|
78 |
|
---|
79 | /* logs list creation */
|
---|
80 | mLogList = new QTabWidget (mLogsFrame, "mLogList");
|
---|
81 | QVBoxLayout *logsFrameLayout = new QVBoxLayout (mLogsFrame);
|
---|
82 | logsFrameLayout->addWidget (mLogList);
|
---|
83 |
|
---|
84 | /* applying language settings */
|
---|
85 | languageChangeImp();
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | void VBoxVMLogViewer::destroy()
|
---|
90 | {
|
---|
91 | mSelf = 0;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | void VBoxVMLogViewer::setup (CMachine &aMachine)
|
---|
96 | {
|
---|
97 | /* saving related machine */
|
---|
98 | mMachine = aMachine;
|
---|
99 |
|
---|
100 | /* reading log files */
|
---|
101 | refresh();
|
---|
102 |
|
---|
103 | /* loading language constants */
|
---|
104 | languageChangeImp();
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | const CMachine& VBoxVMLogViewer::machine()
|
---|
109 | {
|
---|
110 | return mMachine;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | void VBoxVMLogViewer::languageChangeImp()
|
---|
115 | {
|
---|
116 | /* setup a dialog caption */
|
---|
117 | if (!mMachine.isNull())
|
---|
118 | setCaption (tr ("%1 - VirtualBox Log Viewer").arg (mMachine.GetName()));
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | QPushButton* VBoxVMLogViewer::searchDefaultButton()
|
---|
123 | {
|
---|
124 | /* this mechanism is used for searching the default dialog button
|
---|
125 | * and similar the same mechanism in Qt::QDialog inner source */
|
---|
126 | QPushButton *button = 0;
|
---|
127 | QObjectList *list = queryList ("QPushButton");
|
---|
128 | QObjectListIt it (*list);
|
---|
129 | while ((button = (QPushButton*)it.current()) && !button->isDefault())
|
---|
130 | ++ it;
|
---|
131 | return button;
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | bool VBoxVMLogViewer::eventFilter (QObject *aObject, QEvent *aEvent)
|
---|
136 | {
|
---|
137 | switch (aEvent->type())
|
---|
138 | {
|
---|
139 | /* auto-default button focus-in processor used to move the "default"
|
---|
140 | * button property into the currently focused button */
|
---|
141 | case QEvent::FocusIn:
|
---|
142 | {
|
---|
143 | if (aObject->inherits ("QPushButton") &&
|
---|
144 | aObject->parent() == centralWidget())
|
---|
145 | {
|
---|
146 | ((QPushButton*)aObject)->setDefault (aObject != mDefaultButton);
|
---|
147 | if (mDefaultButton)
|
---|
148 | mDefaultButton->setDefault (aObject == mDefaultButton);
|
---|
149 | }
|
---|
150 | break;
|
---|
151 | }
|
---|
152 | /* auto-default button focus-out processor used to remove the "default"
|
---|
153 | * button property from the previously focused button */
|
---|
154 | case QEvent::FocusOut:
|
---|
155 | {
|
---|
156 | if (aObject->inherits ("QPushButton") &&
|
---|
157 | aObject->parent() == centralWidget())
|
---|
158 | {
|
---|
159 | if (mDefaultButton)
|
---|
160 | mDefaultButton->setDefault (aObject != mDefaultButton);
|
---|
161 | ((QPushButton*)aObject)->setDefault (aObject == mDefaultButton);
|
---|
162 | }
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | default:
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | return QMainWindow::eventFilter (aObject, aEvent);
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | bool VBoxVMLogViewer::event (QEvent *aEvent)
|
---|
173 | {
|
---|
174 | bool result = QMainWindow::event (aEvent);
|
---|
175 | switch (aEvent->type())
|
---|
176 | {
|
---|
177 | case QEvent::LanguageChange:
|
---|
178 | {
|
---|
179 | languageChangeImp();
|
---|
180 | break;
|
---|
181 | }
|
---|
182 | default:
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | return result;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | void VBoxVMLogViewer::keyPressEvent (QKeyEvent *aEvent)
|
---|
190 | {
|
---|
191 | if (aEvent->state() == 0 ||
|
---|
192 | (aEvent->state() & Keypad && aEvent->key() == Key_Enter))
|
---|
193 | {
|
---|
194 | switch (aEvent->key())
|
---|
195 | {
|
---|
196 | /* processing the return keypress for the auto-default button */
|
---|
197 | case Key_Enter:
|
---|
198 | case Key_Return:
|
---|
199 | {
|
---|
200 | QPushButton *currentDefault = searchDefaultButton();
|
---|
201 | if (currentDefault)
|
---|
202 | currentDefault->animateClick();
|
---|
203 | break;
|
---|
204 | }
|
---|
205 | /* processing the escape keypress as the close dialog action */
|
---|
206 | case Key_Escape:
|
---|
207 | {
|
---|
208 | close();
|
---|
209 | break;
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 | else
|
---|
214 | aEvent->ignore();
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | void VBoxVMLogViewer::showEvent (QShowEvent *aEvent)
|
---|
219 | {
|
---|
220 | QMainWindow::showEvent (aEvent);
|
---|
221 |
|
---|
222 | /* one may think that QWidget::polish() is the right place to do things
|
---|
223 | * below, but apparently, by the time when QWidget::polish() is called,
|
---|
224 | * the widget style & layout are not fully done, at least the minimum
|
---|
225 | * size hint is not properly calculated. Since this is sometimes necessary,
|
---|
226 | * we provide our own "polish" implementation. */
|
---|
227 |
|
---|
228 | if (mIsPolished)
|
---|
229 | return;
|
---|
230 |
|
---|
231 | mIsPolished = true;
|
---|
232 |
|
---|
233 | VBoxGlobal::centerWidget (this, parentWidget());
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | void VBoxVMLogViewer::resizeEvent (QResizeEvent*)
|
---|
238 | {
|
---|
239 | /* adjust the size-grip location for the current resize event */
|
---|
240 | mSizeGrip->move (centralWidget()->rect().bottomRight() -
|
---|
241 | QPoint (mSizeGrip->rect().width() - 1,
|
---|
242 | mSizeGrip->rect().height() - 1));
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | void VBoxVMLogViewer::refresh()
|
---|
247 | {
|
---|
248 | /* clearing old data if any */
|
---|
249 | mLogFilesList.clear();
|
---|
250 | while (mLogList->count())
|
---|
251 | {
|
---|
252 | QWidget *logPage = mLogList->page (0);
|
---|
253 | mLogList->removePage (logPage);
|
---|
254 | delete logPage;
|
---|
255 | }
|
---|
256 |
|
---|
257 | bool isAnyLogPresent = false;
|
---|
258 |
|
---|
259 | /* entering log files folder */
|
---|
260 | QString logFilesPath = mMachine.GetLogFolder();
|
---|
261 | QDir logFilesDir (logFilesPath);
|
---|
262 | if (logFilesDir.exists())
|
---|
263 | {
|
---|
264 | /* reading log files folder */
|
---|
265 | QStringList logList = logFilesDir.entryList (QDir::Files);
|
---|
266 | if (!logList.empty()) isAnyLogPresent = true;
|
---|
267 | for (QStringList::Iterator it = logList.begin(); it != logList.end(); ++it)
|
---|
268 | loadLogFile (logFilesDir.filePath (*it));
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* create an empty log page if there are no logs at all */
|
---|
272 | if (!isAnyLogPresent)
|
---|
273 | {
|
---|
274 | QTextBrowser *dummyLog = createLogPage ("VBox.log");
|
---|
275 | dummyLog->setTextFormat (Qt::RichText);
|
---|
276 | dummyLog->setWordWrap (QTextEdit::WidgetWidth);
|
---|
277 | dummyLog->setText (tr ("<p>No log files found. Press the <b>Refresh</b> "
|
---|
278 | "button to rescan the log folder <nobr><b>%1</b></nobr>.</p>")
|
---|
279 | .arg (logFilesPath));
|
---|
280 | /* we don't want it to remain white */
|
---|
281 | dummyLog->setPaper (backgroundBrush());
|
---|
282 | }
|
---|
283 |
|
---|
284 | /* restore previous tab-widget margin which was reseted when
|
---|
285 | * the tab widget's children was removed */
|
---|
286 | mLogList->setMargin (10);
|
---|
287 |
|
---|
288 | /* show the first tab widget's page after the refresh */
|
---|
289 | mLogList->showPage (mLogList->page(0));
|
---|
290 |
|
---|
291 | /* enable/disable save button & tab widget according log presence */
|
---|
292 | mSaveButton->setEnabled (isAnyLogPresent);
|
---|
293 | mLogList->setEnabled (isAnyLogPresent);
|
---|
294 |
|
---|
295 | if (mFirstRun)
|
---|
296 | {
|
---|
297 | /* resize the whole log-viewer to fit 80 symbols in text-browser for
|
---|
298 | * the first time started */
|
---|
299 | QTextBrowser *firstPage = static_cast <QTextBrowser *> (mLogList->page(0));
|
---|
300 | int fullWidth = firstPage->fontMetrics().width (QChar ('x')) * 80 +
|
---|
301 | firstPage->verticalScrollBar()->width() +
|
---|
302 | firstPage->frameWidth() * 2 +
|
---|
303 | 5 + 4 /* left text margin + QTabWidget frame width */ +
|
---|
304 | mLogList->margin() * 2 +
|
---|
305 | centralWidget()->layout()->margin() * 2;
|
---|
306 | resize (fullWidth, height());
|
---|
307 | mFirstRun = false;
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | void VBoxVMLogViewer::loadLogFile (const QString &aFileName)
|
---|
313 | {
|
---|
314 | /* prepare log file */
|
---|
315 | QFile logFile (aFileName);
|
---|
316 | if (!logFile.exists() || !logFile.open (IO_ReadOnly))
|
---|
317 | return;
|
---|
318 |
|
---|
319 | /* read log file and write it into the log page */
|
---|
320 | QTextBrowser *logViewer = createLogPage (QFileInfo (aFileName).fileName());
|
---|
321 | logViewer->setText (logFile.readAll());
|
---|
322 |
|
---|
323 | mLogFilesList << aFileName;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | QTextBrowser* VBoxVMLogViewer::createLogPage (const QString &aName)
|
---|
328 | {
|
---|
329 | QTextBrowser *logViewer = new QTextBrowser();
|
---|
330 | logViewer->setTextFormat (Qt::PlainText);
|
---|
331 | QFont font = logViewer->currentFont();
|
---|
332 | font.setFamily ("Courier New,courier");
|
---|
333 | logViewer->setFont (font);
|
---|
334 | logViewer->setWordWrap (QTextEdit::NoWrap);
|
---|
335 | logViewer->setVScrollBarMode (QScrollView::AlwaysOn);
|
---|
336 | mLogList->addTab (logViewer, aName);
|
---|
337 | return logViewer;
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | void VBoxVMLogViewer::save()
|
---|
342 | {
|
---|
343 | /* prepare "save as" dialog */
|
---|
344 | QDate fullDate = QDate::currentDate();
|
---|
345 | QTime fullTime = QTime::currentTime();
|
---|
346 | QString date = fullDate.toString ("yyyy-MM-dd");
|
---|
347 | QString time = fullTime.toString ("hh-mm-ss");
|
---|
348 | QString defaultFileName = QString ("%1-%2-%3.log")
|
---|
349 | .arg (mMachine.GetName()).arg (date).arg (time);
|
---|
350 | QString defaultFullName = QDir::convertSeparators (QDir::home().absPath() +
|
---|
351 | "/" + defaultFileName);
|
---|
352 |
|
---|
353 | QString newFileName = QFileDialog::getSaveFileName (defaultFullName,
|
---|
354 | QString::null, this, "SaveLogAsDialog", tr ("Save VirtualBox Log As"));
|
---|
355 |
|
---|
356 | /* save new log into the file */
|
---|
357 | if (!newFileName.isEmpty())
|
---|
358 | {
|
---|
359 | /* reread log data */
|
---|
360 | QFile oldFile (mLogFilesList [mLogList->currentPageIndex()]);
|
---|
361 | QFile newFile (newFileName);
|
---|
362 | if (!oldFile.open (IO_ReadOnly) || !newFile.open (IO_WriteOnly))
|
---|
363 | return;
|
---|
364 |
|
---|
365 | /* save log data into the new file */
|
---|
366 | newFile.writeBlock (oldFile.readAll());
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|