Installer import into contrib (real import this time)
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / frontends / qt / src / mainwindow.cpp
1 /*
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Shawn R. Walker <adonijah@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $Id: mainwindow.cpp,v 1.19 2004/11/06 18:17:51 cpressey Exp $
35  * $DragonFly: src/contrib/bsdinstaller-1.1.6/src/frontends/qt/src/mainwindow.cpp,v 1.1.1.1 2008/03/12 22:15:53 dave Exp $
36  */
37
38 #include <stdio.h>
39 #include <iostream>
40
41 #include "form.h"
42 #include "mainwindow.h"
43
44 MainWindow::MainWindow(QWidget *parent, const char *name)
45     : QMainWindow(parent, name)
46 {
47         myTimerId = 0;
48
49         createActions();
50         createMenus();
51
52         setCaption(tr("The BSD Installer"));
53         setIcon(QPixmap::fromMimeSource("icon.png"));
54
55         connection = dfui_connection_new(DFUI_TRANSPORT_TCP, "9999");
56         dfui_fe_connect(connection);
57
58         resize(640, 480);
59
60         form = new Form(this, NULL, connection);
61         setCentralWidget(form);
62 }
63
64 void MainWindow::createActions()
65 {
66         exitAct = new QAction(tr("E&xit"), tr("Ctrl+Q"), this);
67         exitAct->setStatusTip(tr("Exit the application"));
68         connect(exitAct, SIGNAL(activated()), this, SLOT(close()));
69
70         aboutAct = new QAction(tr("&About"), 0, this);
71         aboutAct->setStatusTip(tr("Show the application's About box"));
72         connect(aboutAct, SIGNAL(activated()), this, SLOT(about()));
73
74         aboutQtAct = new QAction(tr("About &Qt"), 0, this);
75         aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
76         connect(aboutQtAct, SIGNAL(activated()), qApp, SLOT(aboutQt()));
77 }
78
79 void MainWindow::createMenus()
80 {
81         fileMenu = new QPopupMenu(this);
82         exitAct->addTo(fileMenu);
83
84         helpMenu = new QPopupMenu(this);
85         aboutAct->addTo(helpMenu);
86         aboutQtAct->addTo(helpMenu);
87
88         menuBar()->insertItem(tr("&File"), fileMenu);
89         menuBar()->insertSeparator();
90         menuBar()->insertItem(tr("&Help"), helpMenu);
91 }
92
93 bool MainWindow::message(DFUI_MSG msg, const char *message, bool confirm)
94 {
95         int ret = 0;
96
97         if (msg == DFUI_INFORMATION) {
98                 ret = QMessageBox::information(this, tr("Information"), tr(message),
99                         confirm ? QMessageBox::Yes : NULL,
100                         confirm ? QMessageBox::No | QMessageBox::Default : NULL);
101         } else if (msg == DFUI_QUESTION) {
102                 ret = QMessageBox::question(this, tr("Question"), tr(message),
103                         confirm ? QMessageBox::Yes : NULL,
104                         confirm ? QMessageBox::No | QMessageBox::Default : NULL);
105         } else if (msg == DFUI_WARNING) {
106                 ret = QMessageBox::warning(this, tr("Warning"), tr(message),
107                         confirm ? QMessageBox::Yes : NULL,
108                         confirm ? QMessageBox::No | QMessageBox::Default : NULL);
109         } else if (msg == DFUI_CRITICAL) {
110                 ret = QMessageBox::critical(this, tr("Critical"), tr(message),
111                         confirm ? QMessageBox::Yes : NULL,
112                         confirm ? QMessageBox::No | QMessageBox::Default : NULL);
113         }
114
115         if (ret == QMessageBox::Yes || ret == QMessageBox::Ok)
116                 return true;
117         else
118                 return false;
119 }
120
121 void MainWindow::about()
122 {
123         QMessageBox::about(this, tr("About The BSD Installer"),
124             tr("<h2>The BSD Installer 1.0</h2>"
125             "<p>Copyright &copy; 2004 The DragonFly Project.</p>"));
126 }
127
128 void MainWindow::showEvent(QShowEvent *)
129 {
130         myTimerId = startTimer(150); // Process dfuibe events every 150ms
131 }
132
133 void MainWindow::closeEvent(QCloseEvent *event)
134 {
135         if (message(DFUI_CRITICAL, tr("Are you sure you want to exit?"))) {
136                 event->accept();
137                 killTimer(myTimerId);
138                 dfui_fe_abort(connection);
139                 dfui_fe_disconnect(connection);
140         } else {
141                 event->ignore();
142         }
143 }
144
145 void MainWindow::timerEvent(QTimerEvent *event)
146 {
147         QString str;
148         void *payload;
149         struct dfui_property *gp;
150         char msgtype;
151
152         if ((event->timerId() == myTimerId) && !form->processing) {
153                 dfui_fe_receive(connection, &msgtype, &payload);
154
155                 switch(msgtype) {
156                 case DFUI_BE_MSG_PRESENT:
157                         form->present((struct dfui_form *)payload);
158                         break;
159
160                 case DFUI_BE_MSG_PROG_BEGIN:
161                         form->present((struct dfui_progress *)payload);
162                         dfui_progress_free((struct dfui_progress *)payload);
163                         dfui_fe_progress_continue(connection);
164                         qApp->processEvents();
165                         break;
166
167                 case DFUI_BE_MSG_PROG_UPDATE:
168                         qApp->processEvents();
169                         form->setProgress(dfui_progress_get_amount(
170                             (struct dfui_progress *)payload));
171
172                         if (dfui_progress_get_streaming((struct dfui_progress *)payload)) {
173                                 form->updateProgressText(
174                                     dfui_progress_get_msg_line(
175                                     (struct dfui_progress *)payload));
176                         }
177
178                         if (form->cancelled)
179                                 dfui_fe_progress_cancel(connection);
180                         else
181                                 dfui_fe_progress_continue(connection);
182
183                         dfui_progress_free((struct dfui_progress *)payload);
184                         qApp->processEvents();
185                         break;
186
187                 case DFUI_BE_MSG_PROG_END:
188                         form->setProgress(-1);
189                         dfui_fe_progress_continue(connection);
190                         qApp->processEvents();
191                         break;
192
193                 case DFUI_BE_MSG_SET_GLOBAL:
194                         gp = (struct dfui_property *)payload;
195
196                         /*
197                          * Check for a change to the "lang" setting...
198                          */
199                         if (strcmp(dfui_property_get_name(gp), "lang") == 0) {
200                                 /*
201                                  * TODO: call the appropriate gettext function.
202                                  */
203                         }
204
205                         dfui_fe_confirm_set_global(connection);
206                         dfui_property_free(gp);
207                         break;
208
209                 case DFUI_BE_MSG_STOP:
210                         dfui_fe_confirm_stop(connection);
211                         dfui_fe_disconnect(connection);
212                         qApp->exit(0);
213                         break;
214                 }
215         }
216 }