summaryrefslogtreecommitdiffstats
path: root/src/javascriptinterface.cpp
blob: 18949aeca8be9236197008090c8c9f4903e46646 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "fbgui.h"
#include "javascriptinterface.h"
#include "sysinfo.h"

//-------------------------------------------------------------------------------------------------------
//                                         Initialisation
//-------------------------------------------------------------------------------------------------------
/**
 * A constructor.
 *
 * @param parent
 *   Is of type QWebFrame.
 */
JavascriptInterface::JavascriptInterface(QWebFrame *parent) {
   qxtLog->debug() << "Initializing javascript interface...";
   _parent = parent;
}
//-------------------------------------------------------------------------------------------------------
/**
 * An empty destructor.
 */
JavascriptInterface::~JavascriptInterface() { /* destructor dummy */
}
//-------------------------------------------------------------------------------------------------------
/**
 * Attaches an instance of this class to the DOM of the HTML page.
 *
 * Attaches an instance of this class to the DOM of the HTML page.
 * This enables the possibility to call slots/methods of this class in
 * JavaScript functions of HTML page. It also calls the
 * JavascriptInterface::loadJQuery() method.
 *
 * @see JavascriptInterface::loadJQuery()
 */
void JavascriptInterface::attachToDOM() {
   _parent->addToJavaScriptWindowObject(QString("fbgui"), this);
   loadJQuery();
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method load the required jQuery libraries into the HTML page.
 *
 * This method load the required jQuery libraries into the HTML page.
 * The libraries are contained in the fbgui.qrc file.
 * The Path to the files is: ":/html/js".
 * Each library will be read and loaded into the HTML page via
 * the evaluateJavaScript() method.
 *
 * @see JavascriptInterface::attachToDOM()
 */
void JavascriptInterface::loadJQuery() {
   QString js;
   QString pathToJsDir(DEFAULT_QRC_HTML_DIR);
   pathToJsDir.append("/js");

   QDir qrcJSDir(pathToJsDir);
   QFileInfoList fiList = qrcJSDir.entryInfoList();
   QFileInfo fi;
   foreach(fi, fiList)
      {
         if (fi.suffix() == "js") {
            //qDebug()<< fi.fileName();
            //qxtLog->debug() << fi.fileName();
            if (fi.fileName() != "test.js") {
               QFile file;
               file.setFileName(pathToJsDir + "/" + fi.fileName());
               file.open(QIODevice::ReadOnly);
               js = file.readAll();
               file.close();

               _parent->evaluateJavaScript(js);
               //qxtLog->debug() << "evaluated " + fi.fileName();
            }
         }
      }
}
//-------------------------------------------------------------------------------------------------------
//                                Javascript functions for webpage
//-------------------------------------------------------------------------------------------------------
/**
 * This method start a download.
 *
 * This method start a download.
 * Can be called from inside a JavaScript function of the HTML page.
 * Emits the JavascriptInterface::requestFile(const QString) signal.
 */
void JavascriptInterface::startDownload(const QString& filename) {
   // ignore if empty filename
   if (filename.isEmpty()) {
      _parent->evaluateJavaScript("alert(\"No filename!\")");
      return;
   }
   emit requestFile(filename);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method start a download.
 *
 * This method start a download.
 * Can be called from inside a JavaScript function of the HTML page.
 *
 * @todo add some more informations
 */
void JavascriptInterface::setCallbackOnFinished(const QString& function) {
   qxtLog->debug() << "[jsi] Callback set: " << function;
   _callbackOnDownloadsFinished = QString(function);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method delivers system informations.
 *
 * This method delivers system informations. Type of informations, are defined by
 * the parameter. The output of this method depends on the parameter.
 * Can be called from inside a JavaScript function of the HTML page.
 *
 * @param infoName
 *   Is of type QString. Defines which method will be called. Possible values are:
 *     - mac
 *     - ip
 *     - mbserial
 *     - usb
 *
 * @return QString
 *   the output of the called method or "info_error" if an error occurred
 *   (e. g. invalid parameter).
 *
 * @see SysInfo::getInfo(const QString& infoName)
 */
const QString JavascriptInterface::getSysInfo(const QString& info) {
   SysInfo si;
   return si.getInfo(info);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method quits the whole program.
 *
 * This method quits the whole program.
 * Can be called from inside a JavaScript function of the HTML page.
 * Emits JavascriptInterface::quitFbgui() signal
 */
void JavascriptInterface::quit() {
   emit quitFbgui();
}

//-------------------------------------------------------------------------------------------------------
/**
 * This method performs a shutdown of the client.
 *
 * This method performs a shutdown of the client.
 * Emits the JavascriptInterface::shutDownClient() signal.It is
 * connected with the fbgui::performShutDown() method.
 *
 * @see fbgui::performShutDown()
 */
void JavascriptInterface::shutDown() {
   emit shutDownClient();
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method performs a reboot of the client.
 *
 * This method performs a reboot of the client.
 * Emits the JavascriptInterface::rebootClient() signal. It is
 * connected with the fbgui::performReboot() method.
 *
 * @see fbgui::performReboot()
 */
void JavascriptInterface::reboot() {
   emit rebootClient();
}
//-------------------------------------------------------------------------------------------------------
//                            Download Manager information exchange
//-------------------------------------------------------------------------------------------------------
/**
 * This method delivers some informations about the downloading file.
 *
 * This method delivers some informations about the downloading file.
 *
 * @todo add some more informations
 */
void JavascriptInterface::downloadInfo(const QString& filename,
      const double& filesize) {
   QString code = QString("downloadInfo('\%1', \%2)").arg(filename).arg(
         filesize);
   _parent->evaluateJavaScript(code);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method updates the progress bar.
 *
 * This method updates the progress bar of the HTML page when an download
 * happens.
 *
 * @todo add some more informations
 */
void JavascriptInterface::updateProgressBar(const int& percent,
      const double& speed, const QString& unit) {
   if (percent == 0)
      return;
   QString code = QString("updateProgress(\%1, \%2, '\%3')").arg(percent).arg(
         speed).arg(unit);
   _parent->evaluateJavaScript(code);
}
//-------------------------------------------------------------------------------------------------------
/**
 * This method sends notifications.
 *
 * @todo add some more informations.
 */
void JavascriptInterface::notify(const QString& msg) {
   qxtLog->debug() << "[jsi] Notifying: " << msg;
   QString code = QString("notify('\%1')").arg(msg);
   _parent->evaluateJavaScript(code);
}
//-------------------------------------------------------------------------------------------------------
/**
 * @todo add some more informations
 */
void JavascriptInterface::callbackOnFinished() {
   QString code = QString("\%1").arg(_callbackOnDownloadsFinished);
   _parent->evaluateJavaScript(code);
}
//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
/**
 * This method triggers the arriving of the IP address
 *
 * Used for testing. This method triggers the arriving of the IP address.
 * Not needed in the final version since the here simulated event will be
 * fired automaticali by the udhcpc command.
 * This methods writes some data into a specific file. This file is watched by an
 * other process which will fire an event as soon as the file changes.
 *
 * @see fbgui::watchForTrigger()
 * @see fbgui::checkForTrigger(const QString& dirname)
 * @see bool fbgui::checkHost()
 * @see void fbgui::loadURL()
 */
void JavascriptInterface::trigger() {
   QFile file(fileToTriggerURL);
   if (file.open(QIODevice::WriteOnly)) {
      file.write("data\n");
      qxtLog->debug() << "[jsi] *trigger watcher*";
   }
   file.close();
}