Diffstat (limited to 'noncore/settings/aqpkg/datamgr.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/aqpkg/datamgr.cpp | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/noncore/settings/aqpkg/datamgr.cpp b/noncore/settings/aqpkg/datamgr.cpp new file mode 100644 index 0000000..7f724af --- a/dev/null +++ b/noncore/settings/aqpkg/datamgr.cpp | |||
@@ -0,0 +1,197 @@ | |||
1 | /*************************************************************************** | ||
2 | datamgr.cpp - description | ||
3 | ------------------- | ||
4 | begin : Thu Aug 29 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 | #include <fstream> | ||
18 | #include <iostream> | ||
19 | using namespace std; | ||
20 | |||
21 | #include <stdio.h> | ||
22 | |||
23 | #include "datamgr.h" | ||
24 | #include "global.h" | ||
25 | |||
26 | |||
27 | DataManager::DataManager() | ||
28 | { | ||
29 | activeServer = ""; | ||
30 | } | ||
31 | |||
32 | DataManager::~DataManager() | ||
33 | { | ||
34 | } | ||
35 | |||
36 | Server *DataManager :: getServer( const char *name ) | ||
37 | { | ||
38 | Server *s = 0; | ||
39 | vector<Server>::iterator it = serverList.begin(); | ||
40 | while ( it != serverList.end() && s == 0 ) | ||
41 | { | ||
42 | if ( it->getServerName() == name ) | ||
43 | s = &(*it); | ||
44 | |||
45 | ++it; | ||
46 | } | ||
47 | |||
48 | return s; | ||
49 | } | ||
50 | |||
51 | Destination *DataManager :: getDestination( const char *name ) | ||
52 | { | ||
53 | Destination *d = 0; | ||
54 | vector<Destination>::iterator it = destList.begin(); | ||
55 | while ( it != destList.end() && d == 0 ) | ||
56 | { | ||
57 | if ( it->getDestinationName() == name ) | ||
58 | d = &(*it); | ||
59 | |||
60 | ++it; | ||
61 | } | ||
62 | |||
63 | return d; | ||
64 | } | ||
65 | |||
66 | void DataManager :: loadServers() | ||
67 | { | ||
68 | // First add our local server - not really a server but | ||
69 | // the local config (which packages are installed) | ||
70 | serverList.push_back( Server( LOCAL_SERVER, "" ) ); | ||
71 | serverList.push_back( Server( LOCAL_IPKGS, "" ) ); | ||
72 | |||
73 | // Read file from /etc/ipkg.conf | ||
74 | QString ipkg_conf = IPKG_CONF; | ||
75 | FILE *fp; | ||
76 | fp = fopen( ipkg_conf, "r" ); | ||
77 | char line[130]; | ||
78 | QString lineStr; | ||
79 | if ( fp == NULL ) | ||
80 | { | ||
81 | cout << "Couldn't open " << ipkg_conf << "! err = " << fp << endl; | ||
82 | return; | ||
83 | } | ||
84 | else | ||
85 | { | ||
86 | while ( fgets( line, sizeof line, fp) != NULL ) | ||
87 | { | ||
88 | lineStr = line; | ||
89 | if ( lineStr.startsWith( "src" ) || lineStr.startsWith( "#src" ) ) | ||
90 | { | ||
91 | char alias[20]; | ||
92 | char url[100]; | ||
93 | sscanf( lineStr, "%*[^ ] %s %s", alias, url ); | ||
94 | Server s( alias, url ); | ||
95 | serverList.push_back( s ); | ||
96 | |||
97 | if ( lineStr.startsWith( "src" ) ) | ||
98 | setActiveServer( alias ); | ||
99 | } | ||
100 | else if ( lineStr.startsWith( "dest" ) ) | ||
101 | { | ||
102 | char alias[20]; | ||
103 | char path[50]; | ||
104 | sscanf( lineStr, "%*[^ ] %s %s", alias, path ); | ||
105 | Destination d( alias, path ); | ||
106 | destList.push_back( d ); | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | fclose( fp ); | ||
111 | |||
112 | // Go through the server destination list and add root, cf and card if they | ||
113 | // don't already exist | ||
114 | vector<Destination>::iterator dit; | ||
115 | bool foundRoot = false; | ||
116 | bool foundCF = false; | ||
117 | bool foundCard = false; | ||
118 | for ( dit = destList.begin() ; dit != destList.end() ; ++dit ) | ||
119 | { | ||
120 | if ( dit->getDestinationPath() == "/" ) | ||
121 | foundRoot = true; | ||
122 | if ( dit->getDestinationPath() == "/mnt/cf" ) | ||
123 | foundCF = true; | ||
124 | if ( dit->getDestinationPath() == "/mnt/card" ) | ||
125 | foundCard = true; | ||
126 | } | ||
127 | |||
128 | // If running on a Zaurus (arm) then if we didn't find root, CF or card | ||
129 | // destinations, add them as default | ||
130 | #ifdef QWS | ||
131 | #ifndef X86 | ||
132 | if ( !foundRoot ) | ||
133 | destList.push_back( Destination( "root", "/" ) ); | ||
134 | if ( !foundCF ) | ||
135 | destList.push_back( Destination( "cf", "/mnt/cf" ) ); | ||
136 | if ( !foundCF ) | ||
137 | destList.push_back( Destination( "card", "/mnt/card" ) ); | ||
138 | #endif | ||
139 | #endif | ||
140 | |||
141 | vector<Server>::iterator it; | ||
142 | for ( it = serverList.begin() ; it != serverList.end() ; ++it ) | ||
143 | reloadServerData( it->getServerName() ); | ||
144 | } | ||
145 | |||
146 | void DataManager :: reloadServerData( const char *serverName ) | ||
147 | { | ||
148 | Server *s = getServer( serverName ); | ||
149 | // Now we've read the config file in we need to read the servers | ||
150 | // The local server is a special case. This holds the contents of the | ||
151 | // status files the number of which depends on how many destinations | ||
152 | // we've set up | ||
153 | // The other servers files hold the contents of the server package list | ||
154 | if ( s->getServerName() == LOCAL_SERVER ) | ||
155 | s->readStatusFile( destList ); | ||
156 | else if ( s->getServerName() == LOCAL_IPKGS ) | ||
157 | s->readLocalIpks( getServer( LOCAL_SERVER ) ); | ||
158 | else | ||
159 | s->readPackageFile( getServer( LOCAL_SERVER ) ); | ||
160 | |||
161 | } | ||
162 | |||
163 | void DataManager :: writeOutIpkgConf() | ||
164 | { | ||
165 | QString ipkg_conf = IPKG_CONF; | ||
166 | ofstream out( ipkg_conf ); | ||
167 | |||
168 | out << "# Written by NetworkPackageManager Package Manager" << endl; | ||
169 | |||
170 | // Write out servers | ||
171 | vector<Server>::iterator it = serverList.begin(); | ||
172 | while ( it != serverList.end() ) | ||
173 | { | ||
174 | QString alias = it->getServerName(); | ||
175 | // Don't write out local as its a dummy | ||
176 | if ( alias != LOCAL_SERVER && alias != LOCAL_IPKGS ) | ||
177 | { | ||
178 | QString url = it->getServerUrl();; | ||
179 | |||
180 | if ( !activeServer || alias != activeServer ) | ||
181 | out << "#"; | ||
182 | out << "src " << alias << " " << url << endl; | ||
183 | } | ||
184 | |||
185 | it++; | ||
186 | } | ||
187 | |||
188 | // Write out destinations | ||
189 | vector<Destination>::iterator it2 = destList.begin(); | ||
190 | while ( it2 != destList.end() ) | ||
191 | { | ||
192 | out << "dest " << it2->getDestinationName() << " " << it2->getDestinationPath() << endl; | ||
193 | it2++; | ||
194 | } | ||
195 | |||
196 | out.close(); | ||
197 | } | ||