]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/pages.cpp
Commit of patch by Riccardo.
[bacula/bacula] / bacula / src / qt-console / pages.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 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 John Walker.
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 "pages.h"
35 #include "bat.h"
36
37 /* A global function */
38 bool isWin32Path(QString &fullPath) 
39 {
40    char *buf = fullPath.left(2).toUtf8().data();
41
42    //bool toret = B_ISALPHA(buf[1]);
43    bool toret = buf[1] == ':' && B_ISALPHA(buf[0]);
44    if (mainWin->m_miscDebug) {
45       if (toret)
46          Pmsg1(000, "returning from isWin32Path true %s\n", fullPath.toUtf8().data());
47       else
48          Pmsg1(000, "returning from isWin32Path false %s\n", fullPath.toUtf8().data());
49    }
50    return toret;
51 }
52
53 /*
54  * dockPage
55  * This function is intended to be called from within the Pages class to pull
56  * a window from floating to in the stack widget.
57  */
58
59 void Pages::dockPage()
60 {
61    /* These two lines are for making sure if it is being changed from a window
62     * that it has the proper window flag and parent.
63     */
64    setWindowFlags(Qt::Widget);
65
66    /* This was being done already */
67    m_parent->addWidget(this);
68
69    /* Set docked flag */
70    m_docked = true;
71    mainWin->stackedWidget->setCurrentWidget(this);
72    /* lets set the page selectors action for docking or undocking */
73    setContextMenuDockText();
74 }
75
76 /*
77  * undockPage
78  * This function is intended to be called from within the Pages class to put
79  * a window from the stack widget to a floating window.
80  */
81
82 void Pages::undockPage()
83 {
84    /* Change from a stacked widget to a normal window */
85    m_parent->removeWidget(this);
86    setWindowFlags(Qt::Window);
87    show();
88    /* Clear docked flag */
89    m_docked = false;
90    /* The window has been undocked, lets change the context menu */
91    setContextMenuDockText();
92 }
93
94 /*
95  * This function is intended to be called with the subclasses.  When it is 
96  * called the specific sublclass does not have to be known to Pages.  When it 
97  * is called this function will change the page from it's current state of being
98  * docked or undocked and change it to the other.
99  */
100
101 void Pages::togglePageDocking()
102 {
103    if (m_docked) {
104       undockPage();
105    } else {
106       dockPage();
107    }
108 }
109
110 /*
111  * This function is because I wanted for some reason to keep it protected but still 
112  * give any subclasses the ability to find out if it is currently stacked or not.
113  */
114 bool Pages::isDocked()
115 {
116    return m_docked;
117 }
118
119 /*
120  * To keep m_closeable protected as well
121  */
122 bool Pages::isCloseable()
123 {
124    return m_closeable;
125 }
126
127 /*
128  * When a window is closed, this slot is called.  The idea is to put it back in the
129  * stack here, and it works.  I wanted to get it to the top of the stack so that the
130  * user immediately sees where his window went.  Also, if he undocks the window, then
131  * closes it with the tree item highlighted, it may be confusing that the highlighted
132  * treewidgetitem is not the stack item in the front.
133  */
134
135 void Pages::closeEvent(QCloseEvent* event)
136 {
137    /* A Widget was closed, lets toggle it back into the window, and set it in front. */
138    dockPage();
139
140    /* this fixes my woes of getting the widget to show up on top when closed */
141    event->ignore();
142
143    /* Set the current tree widget item in the Page Selector window to the item 
144     * which represents "this" 
145     * Which will also bring "this" to the top of the stacked widget */
146    setCurrent();
147 }
148
149 /*
150  * The next three are virtual functions.  The idea here is that each subclass will have the
151  * built in virtual function to override if the programmer wants to populate the window
152  * when it it is first clicked.
153  */
154 void Pages::PgSeltreeWidgetClicked()
155 {
156 }
157
158 /*
159  *  Virtual function which is called when this page is visible on the stack.
160  *  This will be overridden by classes that want to populate if they are on the
161  *  top.
162  */
163 void Pages::currentStackItem()
164 {
165 }
166
167 /*
168  * Function to close the stacked page and remove the widget from the
169  * Page selector window
170  */
171 void Pages::closeStackPage()
172 {
173    /* First get the tree widget item and destroy it */
174    QTreeWidgetItem *item=mainWin->getFromHash(this);
175    /* remove the QTreeWidgetItem <-> page from the hash */
176    mainWin->hashRemove(item, this);
177    /* remove the item from the page selector by destroying it */
178    delete item;
179    /* remove this */
180    delete this;
181 }
182
183 /*
184  * Function to set members from the external mainwin and it's overload being
185  * passed a specific QTreeWidgetItem to be it's parent on the tree
186  */
187 void Pages::pgInitialize()
188 {
189    pgInitialize(QString(), NULL);
190 }
191
192 void Pages::pgInitialize(const QString &name)
193 {
194    pgInitialize(name, NULL);
195 }
196
197 void Pages::pgInitialize(const QString &tname, QTreeWidgetItem *parentTreeWidgetItem)
198 {
199    if (tname.size()) {
200       m_name = tname;
201    }
202    m_parent = mainWin->stackedWidget;
203    m_console = mainWin->currentConsole();
204
205    if (!parentTreeWidgetItem) {
206       parentTreeWidgetItem = m_console->directorTreeItem();
207    }
208
209    QTreeWidgetItem *item = new QTreeWidgetItem(parentTreeWidgetItem);
210    QString name; 
211    treeWidgetName(name);
212    item->setText(0, name);
213    mainWin->hashInsert(item, this);
214    setTitle();
215 }
216
217 /*
218  * Virtual Function to return a name
219  * All subclasses should override this function
220  */
221 void Pages::treeWidgetName(QString &name)
222 {
223    name = m_name;
224 }
225
226 /*
227  * Function to simplify executing a console command and bringing the
228  * console to the front of the stack
229  */
230 void Pages::consoleCommand(QString &command)
231 {
232    /*if (!m_console->is_connectedGui())
233        return;*/
234    if (!m_console->preventInUseConnect()) {
235        return;
236    }
237    consoleInput(command);
238 }
239
240 /*
241  * Function to simplify executing a console command, but does not
242  *  check for the connection in use.  We need this so that we can
243  *  *always* enter command from the command line.
244  */
245 void Pages::consoleInput(QString &command)
246 {
247    /* Bring this director's console to the front of the stack */
248    setConsoleCurrent();
249    QString displayhtml("<font color=\"blue\">");
250    displayhtml += command + "</font>\n";
251    m_console->display_html(displayhtml);
252    m_console->display_text("\n");
253    m_console->write_dir(command.toUtf8().data());
254    m_console->displayToPrompt();
255 }
256
257 /*
258  * Function for handling undocked windows becoming active.
259  * Change the currently selected item in the page selector window to the now
260  * active undocked window.  This will also make the console for the undocked
261  * window m_currentConsole.
262  */
263 void Pages::changeEvent(QEvent *event)
264 {
265    if ((event->type() ==  QEvent::ActivationChange) && (isActiveWindow())) {
266       setCurrent();
267    }
268 }
269
270 /*
271  * Function to simplify getting the name of the class and the director into
272  * the caption or title of the window
273  */
274 void Pages::setTitle()
275 {
276    QString wdgname, director;
277    treeWidgetName(wdgname);
278    m_console->getDirResName(director);
279    QString title = tr("%1 of Director %2").arg(wdgname).arg(director);
280    setWindowTitle(title);
281 }
282
283 /*
284  * Bring the current directors console window to the top of the stack.
285  */
286 void Pages::setConsoleCurrent()
287 {
288    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(m_console));
289 }
290
291 /*
292  * Bring this window to the top of the stack.
293  */
294 void Pages::setCurrent()
295 {
296    mainWin->treeWidget->setCurrentItem(mainWin->getFromHash(this));
297 }
298
299 /*
300  * Function to set the text of the toggle dock context menu when page and
301  * widget item are NOT known.  
302  */
303 void Pages::setContextMenuDockText()
304 {
305    QTreeWidgetItem *item = mainWin->getFromHash(this);
306    QString docktext;
307    if (isDocked()) {
308       docktext = tr("UnDock %1 Window").arg(item->text(0));
309    } else {
310       docktext = tr("ReDock %1 Window").arg(item->text(0));
311    }
312       
313    mainWin->actionToggleDock->setText(docktext);
314    setTreeWidgetItemDockColor();
315 }
316
317 /*
318  * Function to set the color of the tree widget item based on whether it is
319  * docked or not.
320  */
321 void Pages::setTreeWidgetItemDockColor()
322 {
323    QTreeWidgetItem* item = mainWin->getFromHash(this);
324    if (item) {
325       if (item->text(0) != tr("Console")) {
326          if (isDocked()) {
327          /* Set the brush to blue if undocked */
328             QBrush blackBrush(Qt::black);
329             item->setForeground(0, blackBrush);
330          } else {
331          /* Set the brush back to black if docked */
332             QBrush blueBrush(Qt::blue);
333             item->setForeground(0, blueBrush);
334          }
335       }
336    }
337 }