]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/pages.cpp
This is committing most of the patch received from Eric.
[bacula/bacula] / bacula / src / qt-console / pages.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *   Version $Id$
30  *
31  *   Dirk Bartley, March 2007
32  */
33
34 #include "bat.h"
35 #include "pages.h"
36
37 /* A global function */
38 bool isWin32Path(QString &fullPath) 
39 {
40    if (fullPath.size()<2) {
41       return false;
42    }
43
44    bool toret = fullPath[1].toAscii() == ':' && fullPath[0].isLetter();
45    if (mainWin->m_miscDebug) {
46       if (toret)
47          Pmsg1(000, "returning from isWin32Path true %s\n", fullPath.toUtf8().data());
48       else
49          Pmsg1(000, "returning from isWin32Path false %s\n", fullPath.toUtf8().data());
50    }
51    return toret;
52 }
53
54 /*
55  * dockPage
56  * This function is intended to be called from within the Pages class to pull
57  * a window from floating to in the stack widget.
58  */
59
60 void Pages::dockPage()
61 {
62    if (isDocked()) {
63       return;
64    }
65
66    /* These two lines are for making sure if it is being changed from a window
67     * that it has the proper window flag and parent.
68     */
69    setWindowFlags(Qt::Widget);
70
71    /* This was being done already */
72    m_parent->addTab(this, m_name);
73
74    /* Set docked flag */
75    m_docked = true;
76    mainWin->tabWidget->setCurrentWidget(this);
77    /* lets set the page selectors action for docking or undocking */
78    setContextMenuDockText();
79
80 }
81
82 /*
83  * undockPage
84  * This function is intended to be called from within the Pages class to put
85  * a window from the stack widget to a floating window.
86  */
87
88 void Pages::undockPage()
89 {
90    if (!isDocked()) {
91       return;
92    }
93
94    /* Change from a stacked widget to a normal window */
95    m_parent->removeTab(m_parent->indexOf(this));
96    setWindowFlags(Qt::Window);
97    show();
98    /* Clear docked flag */
99    m_docked = false;
100    /* The window has been undocked, lets change the context menu */
101    setContextMenuDockText();
102 }
103
104 /*
105  * This function is intended to be called with the subclasses.  When it is 
106  * called the specific sublclass does not have to be known to Pages.  When it 
107  * is called this function will change the page from it's current state of being
108  * docked or undocked and change it to the other.
109  */
110
111 void Pages::togglePageDocking()
112 {
113    if (m_docked) {
114       undockPage();
115    } else {
116       dockPage();
117    }
118 }
119
120 /*
121  * This function is because I wanted for some reason to keep it protected but still 
122  * give any subclasses the ability to find out if it is currently stacked or not.
123  */
124 bool Pages::isDocked()
125 {
126    return m_docked;
127 }
128
129 /*
130  * To keep m_closeable protected as well
131  */
132 bool Pages::isCloseable()
133 {
134    return m_closeable;
135 }
136
137 /*
138  * When a window is closed, this slot is called.  The idea is to put it back in the
139  * stack here, and it works.  I wanted to get it to the top of the stack so that the
140  * user immediately sees where his window went.  Also, if he undocks the window, then
141  * closes it with the tree item highlighted, it may be confusing that the highlighted
142  * treewidgetitem is not the stack item in the front.
143  */
144
145 void Pages::closeEvent(QCloseEvent* event)
146 {
147    /* A Widget was closed, lets toggle it back into the window, and set it in front. */
148    dockPage();
149
150    /* this fixes my woes of getting the widget to show up on top when closed */
151    event->ignore();
152
153    /* Set the current tree widget item in the Page Selector window to the item 
154     * which represents "this" 
155     * Which will also bring "this" to the top of the stacked widget */
156    setCurrent();
157 }
158
159 /*
160  * The next three are virtual functions.  The idea here is that each subclass will have the
161  * built in virtual function to override if the programmer wants to populate the window
162  * when it it is first clicked.
163  */
164 void Pages::PgSeltreeWidgetClicked()
165 {
166 }
167
168 /*
169  *  Virtual function which is called when this page is visible on the stack.
170  *  This will be overridden by classes that want to populate if they are on the
171  *  top.
172  */
173 void Pages::currentStackItem()
174 {
175 }
176
177 /*
178  * Function to close the stacked page and remove the widget from the
179  * Page selector window
180  */
181 void Pages::closeStackPage()
182 {
183    /* First get the tree widget item and destroy it */
184    QTreeWidgetItem *item=mainWin->getFromHash(this);
185    /* remove the QTreeWidgetItem <-> page from the hash */
186    mainWin->hashRemove(item, this);
187    /* remove the item from the page selector by destroying it */
188    delete item;
189    /* remove this */
190    delete this;
191 }
192
193 /*
194  * Function to set members from the external mainwin and it's overload being
195  * passed a specific QTreeWidgetItem to be it's parent on the tree
196  */
197 void Pages::pgInitialize()
198 {
199    pgInitialize(QString(), NULL);
200 }
201
202 void Pages::pgInitialize(const QString &name)
203 {
204    pgInitialize(name, NULL);
205 }
206
207 void Pages::pgInitialize(const QString &tname, QTreeWidgetItem *parentTreeWidgetItem)
208 {
209    m_docked = false;
210    if (tname.size()) {
211       m_name = tname;
212    }
213    m_parent = mainWin->tabWidget;
214    m_console = mainWin->currentConsole();
215
216    if (!parentTreeWidgetItem) {
217       parentTreeWidgetItem = m_console->directorTreeItem();
218    }
219
220    QTreeWidgetItem *item = new QTreeWidgetItem(parentTreeWidgetItem);
221    QString name; 
222    treeWidgetName(name);
223    item->setText(0, name);
224    mainWin->hashInsert(item, this);
225    setTitle();
226 }
227
228 /*
229  * Virtual Function to return a name
230  * All subclasses should override this function
231  */
232 void Pages::treeWidgetName(QString &name)
233 {
234    name = m_name;
235 }
236
237 /*
238  * Function to simplify executing a console command and bringing the
239  * console to the front of the stack
240  */
241 void Pages::consoleCommand(QString &command)
242 {
243    consoleCommand(command, true);
244 }
245 void Pages::consoleCommand(QString &command, bool setCurrent)
246 {
247    int conn;
248    bool donotify = false;
249    if (m_console->availableDirComm(conn))  {
250       if (m_console->is_notify_enabled(conn)) {
251          donotify = true;
252          m_console->notify(conn, false);
253       }
254       consoleCommand(command, conn, setCurrent);
255       if (donotify) { m_console->notify(conn, true); }
256    }
257 }
258 void Pages::consoleCommand(QString &command, int conn)
259 {
260    consoleCommand(command, conn, true);
261 }
262 void Pages::consoleCommand(QString &command, int conn, bool setCurrent)
263 {
264    /* Bring this director's console to the front of the stack */
265    if (setCurrent) { setConsoleCurrent(); }
266    QString displayhtml("<font color=\"blue\">");
267    displayhtml += command + "</font>\n";
268    m_console->display_html(displayhtml);
269    m_console->display_text("\n");
270    mainWin->waitEnter();
271    m_console->write_dir(conn, command.toUtf8().data(), false);
272    m_console->displayToPrompt(conn);
273    mainWin->waitExit();
274 }
275
276 /*
277  * Function for handling undocked windows becoming active.
278  * Change the currently selected item in the page selector window to the now
279  * active undocked window.  This will also make the console for the undocked
280  * window m_currentConsole.
281  */
282 void Pages::changeEvent(QEvent *event)
283 {
284    if ((event->type() ==  QEvent::ActivationChange) && (isActiveWindow())) {
285       setCurrent();
286    }
287 }
288
289 /*
290  * Function to simplify getting the name of the class and the director into
291  * the caption or title of the window
292  */
293 void Pages::setTitle()
294 {
295    QString wdgname, director;
296    treeWidgetName(wdgname);
297    m_console->getDirResName(director);
298    QString title = tr("%1 of Director %2").arg(wdgname).arg(director);
299    setWindowTitle(title);
300 }
301
302 /*
303  * Bring the current directors console window to the top of the stack.
304  */
305 void Pages::setConsoleCurrent()
306 {
307    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(m_console));
308 }
309
310 /*
311  * Bring this window to the top of the stack.
312  */
313 void Pages::setCurrent()
314 {
315    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(this));
316 }
317
318 /*
319  * Function to set the text of the toggle dock context menu when page and
320  * widget item are NOT known.  
321  */
322 void Pages::setContextMenuDockText()
323 {
324    QTreeWidgetItem *item = mainWin->getFromHash(this);
325    QString docktext;
326    if (isDocked()) {
327       docktext = tr("UnDock %1 Window").arg(item->text(0));
328    } else {
329       docktext = tr("ReDock %1 Window").arg(item->text(0));
330    }
331       
332    mainWin->actionToggleDock->setText(docktext);
333    setTreeWidgetItemDockColor();
334 }
335
336 /*
337  * Function to set the color of the tree widget item based on whether it is
338  * docked or not.
339  */
340 void Pages::setTreeWidgetItemDockColor()
341 {
342    QTreeWidgetItem* item = mainWin->getFromHash(this);
343    if (item) {
344       if (item->text(0) != tr("Console")) {
345          if (isDocked()) {
346          /* Set the brush to blue if undocked */
347             QBrush blackBrush(Qt::black);
348             item->setForeground(0, blackBrush);
349          } else {
350          /* Set the brush back to black if docked */
351             QBrush blueBrush(Qt::blue);
352             item->setForeground(0, blueBrush);
353          }
354       }
355    }
356 }
357
358 /* Function to get a list of volumes */
359 void Pages::getVolumeList(QStringList &volumeList)
360 {
361    QString query("SELECT VolumeName AS Media FROM Media ORDER BY Media");
362    if (mainWin->m_sqlDebug) {
363       Pmsg1(000, "Query cmd : %s\n",query.toUtf8().data());
364    }
365    QStringList results;
366    if (m_console->sql_cmd(query, results)) {
367       QString field;
368       QStringList fieldlist;
369       /* Iterate through the lines of results. */
370       foreach (QString resultline, results) {
371          fieldlist = resultline.split("\t");
372          volumeList.append(fieldlist[0]);
373       } /* foreach resultline */
374    } /* if results from query */
375 }
376
377 /* Function to get a list of volumes */
378 void Pages::getStatusList(QStringList &statusLongList)
379 {
380    QString statusQuery("SELECT JobStatusLong FROM Status");
381    if (mainWin->m_sqlDebug) {
382       Pmsg1(000, "Query cmd : %s\n",statusQuery.toUtf8().data());
383    }
384    QStringList statusResults;
385    if (m_console->sql_cmd(statusQuery, statusResults)) {
386       QString field;
387       QStringList fieldlist;
388       /* Iterate through the lines of results. */
389       foreach (QString resultline, statusResults) {
390          fieldlist = resultline.split("\t");
391          statusLongList.append(fieldlist[0]);
392       } /* foreach resultline */
393    } /* if results from statusquery */
394 }