Diffstat (limited to 'noncore/settings/aqpkg/networkpkgmgr.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/settings/aqpkg/networkpkgmgr.cpp | 395 |
1 files changed, 395 insertions, 0 deletions
diff --git a/noncore/settings/aqpkg/networkpkgmgr.cpp b/noncore/settings/aqpkg/networkpkgmgr.cpp new file mode 100644 index 0000000..91a318c --- a/dev/null +++ b/noncore/settings/aqpkg/networkpkgmgr.cpp | |||
@@ -0,0 +1,395 @@ | |||
1 | /*************************************************************************** | ||
2 | networkpkgmgr.cpp - description | ||
3 | ------------------- | ||
4 | begin : Mon Aug 26 13:32:30 BST 2002 | ||
5 | copyright : (C) 2002 by Andy Qua | ||
6 | email : andy.qua@blueyonder.co.uk | ||
7 | ***************************************************************************/ | ||
8 | |||
9 | /*************************************************************************** | ||
10 | * * | ||
11 | * This program is free software; you can redistribute it and/or modify * | ||
12 | * it under the terms of the GNU General Public License as published by * | ||
13 | * the Free Software Foundation; either version 2 of the License, or * | ||
14 | * (at your option) any later version. * | ||
15 | * * | ||
16 | ***************************************************************************/ | ||
17 | |||
18 | #include <fstream> | ||
19 | #include <iostream> | ||
20 | using namespace std; | ||
21 | |||
22 | #include <unistd.h> | ||
23 | #include <stdlib.h> | ||
24 | |||
25 | #ifdef QWS | ||
26 | #include <qpe/qpeapplication.h> | ||
27 | #include <qpe/qcopenvelope_qws.h> | ||
28 | #include <qpe/config.h> | ||
29 | #else | ||
30 | #include <qapplication.h> | ||
31 | #endif | ||
32 | #include <qlabel.h> | ||
33 | #include <qfile.h> | ||
34 | |||
35 | #include "datamgr.h" | ||
36 | #include "networkpkgmgr.h" | ||
37 | #include "installdlgimpl.h" | ||
38 | #include "ipkg.h" | ||
39 | #include "inputdlg.h" | ||
40 | |||
41 | #include "global.h" | ||
42 | |||
43 | NetworkPackageManager::NetworkPackageManager( DataManager *dataManager, QWidget *parent, const char *name) | ||
44 | : QWidget(parent, name) | ||
45 | { | ||
46 | dataMgr = dataManager; | ||
47 | |||
48 | initGui(); | ||
49 | setupConnections(); | ||
50 | |||
51 | progressDlg = 0; | ||
52 | timerId = startTimer( 100 ); | ||
53 | } | ||
54 | |||
55 | NetworkPackageManager::~NetworkPackageManager() | ||
56 | { | ||
57 | } | ||
58 | |||
59 | void NetworkPackageManager :: timerEvent ( QTimerEvent * ) | ||
60 | { | ||
61 | killTimer( timerId ); | ||
62 | |||
63 | // showProgressDialog(); | ||
64 | // Add server names to listbox | ||
65 | updateData(); | ||
66 | |||
67 | // progressDlg->hide(); | ||
68 | } | ||
69 | |||
70 | void NetworkPackageManager :: updateData() | ||
71 | { | ||
72 | serversList->clear(); | ||
73 | packagesList->clear(); | ||
74 | |||
75 | vector<Server>::iterator it; | ||
76 | int activeItem = -1; | ||
77 | int i; | ||
78 | for ( i = 0, it = dataMgr->getServerList().begin() ; it != dataMgr->getServerList().end() ; ++it, ++i ) | ||
79 | { | ||
80 | serversList->insertItem( it->getServerName() ); | ||
81 | if ( it->getServerName() == dataMgr->getActiveServer() ) | ||
82 | activeItem = i; | ||
83 | } | ||
84 | |||
85 | // set selected server to be active server | ||
86 | if ( activeItem != -1 ) | ||
87 | serversList->setCurrentItem( activeItem ); | ||
88 | serverSelected( 0 ); | ||
89 | } | ||
90 | |||
91 | |||
92 | void NetworkPackageManager :: initGui() | ||
93 | { | ||
94 | QLabel *l = new QLabel( "Servers", this ); | ||
95 | serversList = new QComboBox( this ); | ||
96 | packagesList = new QListView( this ); | ||
97 | update = new QPushButton( "Refresh List", this ); | ||
98 | download = new QPushButton( "Download", this ); | ||
99 | apply = new QPushButton( "Apply", this ); | ||
100 | |||
101 | QVBoxLayout *vbox = new QVBoxLayout( this, 0, -1, "VBox" ); | ||
102 | QHBoxLayout *hbox1 = new QHBoxLayout( vbox, -1, "HBox1" ); | ||
103 | hbox1->addWidget( l ); | ||
104 | hbox1->addWidget( serversList ); | ||
105 | |||
106 | vbox->addWidget( packagesList ); | ||
107 | packagesList->addColumn( "Packages" ); | ||
108 | |||
109 | QHBoxLayout *hbox2 = new QHBoxLayout( vbox, -1, "HBox2" ); | ||
110 | hbox2->addWidget( update ); | ||
111 | hbox2->addWidget( download ); | ||
112 | hbox2->addWidget( apply ); | ||
113 | } | ||
114 | |||
115 | void NetworkPackageManager :: setupConnections() | ||
116 | { | ||
117 | connect( serversList, SIGNAL(activated( int )), this, SLOT(serverSelected( int ))); | ||
118 | connect( apply, SIGNAL(released()), this, SLOT(applyChanges()) ); | ||
119 | connect( download, SIGNAL(released()), this, SLOT(downloadPackage()) ); | ||
120 | connect( update, SIGNAL(released()), this, SLOT(updateServer()) ); | ||
121 | } | ||
122 | |||
123 | void NetworkPackageManager :: showProgressDialog() | ||
124 | { | ||
125 | if ( !progressDlg ) | ||
126 | progressDlg = new ProgressDlg( this, "Progress", false ); | ||
127 | progressDlg->setText( "Reading installed packages" ); | ||
128 | progressDlg->show(); | ||
129 | } | ||
130 | |||
131 | |||
132 | void NetworkPackageManager :: serverSelected( int ) | ||
133 | { | ||
134 | packagesList->clear(); | ||
135 | |||
136 | // display packages | ||
137 | QString serverName = serversList->currentText(); | ||
138 | Server *s = dataMgr->getServer( serverName ); | ||
139 | dataMgr->setActiveServer( serverName ); | ||
140 | |||
141 | vector<Package> &list = s->getPackageList(); | ||
142 | vector<Package>::iterator it; | ||
143 | for ( it = list.begin() ; it != list.end() ; ++it ) | ||
144 | { | ||
145 | QString text = ""; | ||
146 | |||
147 | // If the local server, only display installed packages | ||
148 | if ( serverName == LOCAL_SERVER && !it->isInstalled() ) | ||
149 | continue; | ||
150 | |||
151 | text += it->getPackageName(); | ||
152 | if ( it->isInstalled() ) | ||
153 | { | ||
154 | text += " (installed)"; | ||
155 | |||
156 | // If a different version of package is available, postfix it with an * | ||
157 | if ( it->getVersion() != it->getInstalledVersion() ) | ||
158 | text += "*"; | ||
159 | } | ||
160 | |||
161 | QCheckListItem *item = new QCheckListItem( packagesList, text, QCheckListItem::CheckBox ); | ||
162 | if ( !it->isPackageStoredLocally() ) | ||
163 | new QCheckListItem( item, QString( "Description - " ) + it->getDescription() ); | ||
164 | else | ||
165 | new QCheckListItem( item, QString( "Filename - " ) + it->getFilename() ); | ||
166 | |||
167 | new QCheckListItem( item, QString( "V. Available - " ) + it->getVersion() ); | ||
168 | if ( it->getLocalPackage() ) | ||
169 | { | ||
170 | if ( it->isInstalled() ) | ||
171 | new QCheckListItem( item, QString( "V. Installed - " ) + it->getInstalledVersion() ); | ||
172 | } | ||
173 | packagesList->insertItem( item ); | ||
174 | } | ||
175 | |||
176 | // If the local server or the local ipkgs server disable the download button | ||
177 | download->setText( "Download" ); | ||
178 | if ( serverName == LOCAL_SERVER ) | ||
179 | download->setEnabled( false ); | ||
180 | else if ( serverName == LOCAL_IPKGS ) | ||
181 | { | ||
182 | download->setEnabled( true ); | ||
183 | download->setText( "Remove" ); | ||
184 | } | ||
185 | else | ||
186 | download->setEnabled( true ); | ||
187 | } | ||
188 | |||
189 | void NetworkPackageManager :: updateServer() | ||
190 | { | ||
191 | QString serverName = serversList->currentText(); | ||
192 | |||
193 | // Update the current server | ||
194 | // Display dialog | ||
195 | ProgressDlg *dlg = new ProgressDlg( this ); | ||
196 | QString status = "Updating package list for "; | ||
197 | status += serverName; | ||
198 | dlg->show(); | ||
199 | dlg->setText( status ); | ||
200 | |||
201 | // Disable buttons to stop silly people clicking lots on them :) | ||
202 | |||
203 | // First, write out ipkg_conf file so that ipkg can use it | ||
204 | dataMgr->writeOutIpkgConf(); | ||
205 | |||
206 | if ( serverName == LOCAL_SERVER ) | ||
207 | ; | ||
208 | else if ( serverName == LOCAL_IPKGS ) | ||
209 | ; | ||
210 | else | ||
211 | { | ||
212 | QString option = "update"; | ||
213 | QString dummy = ""; | ||
214 | Ipkg ipkg; | ||
215 | connect( &ipkg, SIGNAL(outputText(const QString &)), this, SLOT(displayText(const QString &))); | ||
216 | ipkg.setOption( option ); | ||
217 | |||
218 | ipkg.runIpkg( ); | ||
219 | } | ||
220 | |||
221 | // Reload data | ||
222 | dataMgr->reloadServerData( serversList->currentText() ); | ||
223 | serverSelected(-1); | ||
224 | delete dlg; | ||
225 | } | ||
226 | |||
227 | |||
228 | void NetworkPackageManager :: downloadPackage() | ||
229 | { | ||
230 | if ( download->text() == "Download" ) | ||
231 | { | ||
232 | // First, write out ipkg_conf file so that ipkg can use it | ||
233 | dataMgr->writeOutIpkgConf(); | ||
234 | |||
235 | // Display dialog to user asking where to download the files to | ||
236 | bool ok = FALSE; | ||
237 | QString dir = ""; | ||
238 | #ifdef QWS | ||
239 | // read download directory from config file | ||
240 | Config cfg( "aqpkg" ); | ||
241 | cfg.setGroup( "settings" ); | ||
242 | dir = cfg.readEntry( "downloadDir", "/home/root/Documents/application/ipkg" ); | ||
243 | #endif | ||
244 | |||
245 | QString text = InputDialog::getText( tr( "Download to where" ), tr( "Enter path to download to" ), dir, &ok, this ); | ||
246 | if ( ok && !text.isEmpty() ) | ||
247 | dir = text; // user entered something and pressed ok | ||
248 | else | ||
249 | return; // user entered nothing or pressed cancel | ||
250 | |||
251 | #ifdef QWS | ||
252 | // Store download directory in config file | ||
253 | cfg.writeEntry( "downloadDir", dir ); | ||
254 | #endif | ||
255 | |||
256 | // Get starting directory | ||
257 | char initDir[PATH_MAX]; | ||
258 | getcwd( initDir, PATH_MAX ); | ||
259 | |||
260 | // Download each package | ||
261 | Ipkg ipkg; | ||
262 | connect( &ipkg, SIGNAL(outputText(const QString &)), this, SLOT(displayText(const QString &))); | ||
263 | |||
264 | QCheckListItem *item = (QCheckListItem *)packagesList->firstChild(); | ||
265 | ipkg.setOption( "download" ); | ||
266 | ipkg.setRuntimeDirectory( initDir ); | ||
267 | do | ||
268 | { | ||
269 | if ( item->isOn() ) | ||
270 | { | ||
271 | QString name = item->text(); | ||
272 | int pos = name.find( "*" ); | ||
273 | name.truncate( pos ); | ||
274 | |||
275 | // if (there is a (installed), remove it | ||
276 | pos = name.find( "(installed)" ); | ||
277 | if ( pos > 0 ) | ||
278 | name.truncate( pos - 1 ); | ||
279 | |||
280 | ipkg.setPackage( name ); | ||
281 | ipkg.runIpkg( ); | ||
282 | } | ||
283 | |||
284 | item = (QCheckListItem *)item->nextSibling(); | ||
285 | } while ( item ); | ||
286 | } | ||
287 | else if ( download->text() == "Remove" ) | ||
288 | { | ||
289 | QCheckListItem *item = (QCheckListItem *)packagesList->firstChild(); | ||
290 | do | ||
291 | { | ||
292 | if ( item->isOn() ) | ||
293 | { | ||
294 | QString name = item->text(); | ||
295 | int pos = name.find( "*" ); | ||
296 | name.truncate( pos ); | ||
297 | |||
298 | // if (there is a (installed), remove it | ||
299 | pos = name.find( "(installed)" ); | ||
300 | if ( pos > 0 ) | ||
301 | name.truncate( pos - 1 ); | ||
302 | |||
303 | Package *p = dataMgr->getServer( serversList->currentText() )->getPackage( name ); | ||
304 | QFile f( p->getFilename() ); | ||
305 | f.remove(); | ||
306 | } | ||
307 | item = (QCheckListItem *)item->nextSibling(); | ||
308 | } while ( item ); | ||
309 | } | ||
310 | |||
311 | dataMgr->reloadServerData( LOCAL_IPKGS ); | ||
312 | serverSelected( -1 ); | ||
313 | } | ||
314 | |||
315 | |||
316 | void NetworkPackageManager :: applyChanges() | ||
317 | { | ||
318 | // Disable buttons to stop silly people clicking lots on them :) | ||
319 | |||
320 | // First, write out ipkg_conf file so that ipkg can use it | ||
321 | dataMgr->writeOutIpkgConf(); | ||
322 | |||
323 | // Now for each selected item | ||
324 | // deal with it | ||
325 | |||
326 | vector<QString> workingPackages; | ||
327 | QCheckListItem *item = (QCheckListItem *)packagesList->firstChild(); | ||
328 | do | ||
329 | { | ||
330 | if ( item->isOn() ) | ||
331 | { | ||
332 | QString p = dealWithItem( item ); | ||
333 | workingPackages.push_back( p ); | ||
334 | } | ||
335 | |||
336 | item = (QCheckListItem *)item->nextSibling(); | ||
337 | } while ( item ); | ||
338 | |||
339 | // do the stuff | ||
340 | InstallDlgImpl dlg( workingPackages, dataMgr, this, "Install", true ); | ||
341 | dlg.showDlg(); | ||
342 | |||
343 | // Reload data | ||
344 | dataMgr->reloadServerData( LOCAL_SERVER ); | ||
345 | dataMgr->reloadServerData( serversList->currentText() ); | ||
346 | serverSelected(-1); | ||
347 | |||
348 | #ifdef QWS | ||
349 | // Finally let the main system update itself | ||
350 | QCopEnvelope e("QPE/System", "linkChanged(QString)"); | ||
351 | QString lf = QString::null; | ||
352 | e << lf; | ||
353 | #endif | ||
354 | } | ||
355 | |||
356 | // decide what to do - either remove, upgrade or install | ||
357 | // Current rules: | ||
358 | // If not installed - install | ||
359 | // If installed and different version available - upgrade | ||
360 | // If installed and version up to date - remove | ||
361 | QString NetworkPackageManager :: dealWithItem( QCheckListItem *item ) | ||
362 | { | ||
363 | QString name = item->text(); | ||
364 | int pos = name.find( "*" ); | ||
365 | name.truncate( pos ); | ||
366 | |||
367 | // if (there is a (installed), remove it | ||
368 | pos = name.find( "(installed)" ); | ||
369 | if ( pos > 0 ) | ||
370 | name.truncate( pos - 1 ); | ||
371 | |||
372 | // Get package | ||
373 | Server *s = dataMgr->getServer( serversList->currentText() ); | ||
374 | Package *p = s->getPackage( name ); | ||
375 | |||
376 | // If the package has a filename then it is a local file | ||
377 | if ( p->isPackageStoredLocally() ) | ||
378 | name = p->getFilename(); | ||
379 | QString option; | ||
380 | QString dest = "root"; | ||
381 | if ( !p->isInstalled() ) | ||
382 | return QString( "I" ) + name; | ||
383 | else | ||
384 | { | ||
385 | if ( p->getVersion() == p->getInstalledVersion() ) | ||
386 | return QString( "D" ) + name; | ||
387 | else | ||
388 | return QString( "U" ) + name; | ||
389 | } | ||
390 | } | ||
391 | |||
392 | void NetworkPackageManager :: displayText( const QString &t ) | ||
393 | { | ||
394 | cout << t << endl; | ||
395 | } | ||