00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "server.h"
00031 #include "datamgr.h"
00032
00033 #include <qfile.h>
00034 #include <qtextstream.h>
00035
00036 #ifdef QWS
00037 #include <qpe/global.h>
00038 #include <qpe/applnk.h>
00039 #include <qlist.h>
00040 #endif
00041
00042 #include "utils.h"
00043
00044 #include "global.h"
00045
00046 Server :: Server( const char *name, const char *url )
00047 {
00048 serverName = name;
00049 serverUrl = url;
00050 packageFile = IPKG_DIR;
00051 active = true;
00052 packageFile.append( "lists/" );
00053 packageFile.append( serverName );
00054 }
00055
00056 Server :: ~Server()
00057 {
00058 cleanUp();
00059 }
00060
00061 void Server :: cleanUp()
00062 {
00063 packageList.clear();
00064 }
00065
00066 void Server :: readStatusFile( QList<Destination> &destList )
00067 {
00068 cleanUp();
00069
00070 Destination *dest;
00071 QListIterator<Destination> dit( destList );
00072 bool rootRead = false;
00073 for ( ; dit.current(); ++dit )
00074 {
00075 dest = dit.current();
00076 bool installingToRoot = false;
00077
00078 QString path = dest->getDestinationPath();
00079 if ( path.right( 1 ) != "/" )
00080 path.append( "/" );
00081
00082 if ( path == "/" )
00083 {
00084 rootRead = true;
00085 installingToRoot = true;
00086 }
00087
00088 packageFile = path;
00089 packageFile.append( "usr/lib/ipkg/status" );
00090 readPackageFile( 0, false, installingToRoot, &( *dest ) );
00091 }
00092
00093
00094 if ( !rootRead )
00095 {
00096 packageFile = "/usr/lib/ipkg/status";
00097 readPackageFile( 0, false, true );
00098 }
00099 }
00100
00101 void Server :: readLocalIpks( Server *local )
00102 {
00103 cleanUp();
00104
00105 #ifdef QWS
00106
00107
00108
00109 DocLnkSet files;
00110 Global::findDocuments( &files, "application/ipkg" );
00111
00112
00113 QListIterator<DocLnk> it( files.children() );
00114
00115 for ( ; it.current() ; ++it )
00116 {
00117
00118
00119 QString file = (*it)->file();
00120
00121
00122 QString packageName = Utils::getPackageNameFromIpkFilename( file );
00123 QString ver = Utils::getPackageVersionFromIpkFilename( file );
00124 Package *package = new Package( packageName );
00125 package->setVersion( ver );
00126 package->setFilename( file );
00127 package->setPackageStoredLocally( true );
00128 packageList.append( package );
00129 }
00130 #else
00131 QString names[] = { "advancedfm_0.9.1-20020811_arm.ipk", "libopie_0.9.1-20020811_arm.ipk", "libopieobex_0.9.1-20020811.1_arm.ipk", "opie-addressbook_0.9.1-20020811_arm.ipk" };
00132 for ( int i = 0 ; i < 4 ; ++i )
00133 {
00134
00135
00136 QString file = names[i];
00137 int p = file.find( "_" );
00138 QString tmp = file.mid( 0, p );
00139 packageList.push_back( Package( tmp ) );
00140 int p2 = file.find( "_", p+1 );
00141 tmp = file.mid( p+1, p2-(p+1) );
00142 packageList.back().setVersion( tmp );
00143 packageList.back().setPackageStoredLocally( true );
00144 }
00145 #endif
00146
00147
00148 buildLocalPackages( local );
00149 }
00150
00151 void Server :: readPackageFile( Server *local, bool clearAll, bool installingToRoot, Destination *dest )
00152 {
00153 QFile f( packageFile );
00154 if ( !f.open( IO_ReadOnly ) )
00155 return;
00156 QTextStream t( &f );
00157
00158 QString line;
00159 QString key;
00160 QString value;
00161 int pos;
00162
00163 if ( clearAll )
00164 cleanUp();
00165 Package *currPackage = 0;
00166
00167 bool newPackage = true;
00168 while ( !t.eof() )
00169 {
00170 line = t.readLine();
00171
00172 pos = line.find( ':', 0 );
00173 if ( pos > -1 )
00174 key = line.mid( 0, pos ).stripWhiteSpace();
00175 else
00176 key = QString::null;
00177 value = line.mid( pos+1, line.length()-pos ).stripWhiteSpace();
00178
00179 if ( key == "Package" && newPackage )
00180 {
00181 newPackage = false;
00182
00183 currPackage = getPackage( value );
00184 if ( !currPackage )
00185 {
00186 Package *package = new Package( value );
00187 packageList.append( package );
00188 currPackage = package;
00189 currPackage->setInstalledTo( dest );
00190 if ( installingToRoot )
00191 currPackage->setInstalledToRoot( true );
00192 }
00193 else
00194 {
00195 if ( currPackage->isInstalled() )
00196 currPackage->setInstalledTo( dest );
00197 }
00198 }
00199 else if ( key == "Version" )
00200 {
00201 if ( currPackage )
00202 currPackage->setVersion( value );
00203 }
00204 else if ( key == "Status" )
00205 {
00206 if ( currPackage )
00207 currPackage->setStatus( value );
00208 }
00209 else if ( key == "Description" )
00210 {
00211 if ( currPackage )
00212 currPackage->setDescription( value );
00213 }
00214 else if ( key == "Filename" )
00215 {
00216 if ( currPackage )
00217 currPackage->setFilename( value );
00218 }
00219 else if ( key == "Size" )
00220 {
00221 if ( currPackage )
00222 currPackage->setPackageSize( value );
00223 }
00224 else if ( key == "Section" )
00225 {
00226 if ( currPackage )
00227 currPackage->setSection( value );
00228
00229 DataManager::setAvailableCategories( value );
00230 }
00231 else if ( key == QString::null )
00232 {
00233 newPackage = true;
00234 }
00235 }
00236
00237 f.close();
00238
00239
00240 buildLocalPackages( local );
00241 }
00242
00243 void Server :: buildLocalPackages( Server *local )
00244 {
00245 Package *curr;
00246 QListIterator<Package> it( packageList );
00247
00248 QList<Package> *locallist = &local->getPackageList();
00249
00250 for ( ; it.current(); ++it )
00251 {
00252 curr = it.current();
00253 QString name = curr->getPackageName();
00254
00255
00256 if ( name.find( ".ipk" ) != -1 )
00257 name = Utils::getPackageNameFromIpkFilename( curr->getFilename() );
00258
00259 if ( local )
00260 {
00261 Package *p = local->getPackage( name );
00262 curr->setLocalPackage( p );
00263 if ( p )
00264 {
00265
00266 if ( curr->getVersion() > p->getVersion() )
00267 {
00268 int pos = locallist->at();
00269 locallist->remove( p );
00270 locallist->insert( pos, curr );
00271 }
00272
00273
00274 if ( p->getInstalledVersion() == curr->getVersion() )
00275 {
00276 p->setPackageSize( curr->getPackageSize() );
00277 p->setSection( curr->getSection() );
00278 p->setDescription( curr->getDescription() );
00279 }
00280 }
00281
00282 }
00283 else
00284 curr->setLocalPackage( 0 );
00285 }
00286
00287 }
00288
00289 Package *Server :: getPackage( QString &name )
00290 {
00291 return getPackage( (const char *)name );
00292 }
00293
00294 Package *Server :: getPackage( const char *name )
00295 {
00296 Package *ret = 0;
00297
00298 QListIterator<Package> it( packageList );
00299 for ( ; it.current(); ++it )
00300 {
00301 if ( it.current()->getPackageName() == name )
00302 ret = it.current();
00303 }
00304
00305 return ret;
00306 }
00307
00308 QString Server :: toString()
00309 {
00310 QString ret = QString( "Server\n name - %1\n url - %2\n" ).arg( serverName ).arg( serverUrl );
00311
00312 QListIterator<Package> it( packageList );
00313 for ( ; it.current(); ++it )
00314 {
00315 ret.append( QString( "\n %1" ).arg( it.current()->toString() ) );
00316 }
00317
00318
00319 return ret;
00320 }
00321
00322 QList<Package> &Server::getPackageList()
00323 {
00324 return packageList;
00325 }
00326