Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

datamgr.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the OPIE Project
00003 
00004                =.            Copyright (c)  2002 Andy Qua <andy.qua@blueyonder.co.uk>
00005              .=l.                                Dan Williams <drw@handhelds.org>
00006            .>+-=
00007  _;:,     .>    :=|.         This file is free software; you can
00008 .> <`_,   >  .   <=          redistribute it and/or modify it under
00009 :`=1 )Y*s>-.--   :           the terms of the GNU General Public
00010 .="- .-=="i,     .._         License as published by the Free Software
00011  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00012      ._= =}       :          or (at your option) any later version.
00013     .%`+i>       _;_.
00014     .i_,=:_.      -<s.       This file is distributed in the hope that
00015      +  .  -:.       =       it will be useful, but WITHOUT ANY WARRANTY;
00016     : ..    .:,     . . .    without even the implied warranty of
00017     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00018   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU General
00019 ..}^=.=       =       ;      Public License for more details.
00020 ++=   -.     .`     .:
00021  :     =  ...= . :.=-        You should have received a copy of the GNU
00022  -.   .:....=;==+<;          General Public License along with this file;
00023   -_. . .   )=.  =           see the file COPYING. If not, write to the
00024     --        :-=`           Free Software Foundation, Inc.,
00025                              59 Temple Place - Suite 330,
00026                              Boston, MA 02111-1307, USA.
00027 
00028 */
00029 
00030 #ifdef QWS
00031 #include <qpe/config.h>
00032 #include <qpe/qpeapplication.h>
00033 #else
00034 #include <qapplication.h>
00035 #endif
00036 
00037 #include <qfile.h>
00038 #include <qtextstream.h>
00039 
00040 #include "datamgr.h"
00041 #include "global.h"
00042 
00043 QString LOCAL_SERVER;
00044 QString LOCAL_IPKGS;
00045 
00046 
00047 QString DataManager::availableCategories = "";
00048 DataManager::DataManager()
00049     : QObject( 0x0, 0x0 )
00050 {
00051     activeServer = "";
00052     availableCategories = "#";
00053 
00054     serverList.setAutoDelete( TRUE );
00055     destList.setAutoDelete( TRUE );
00056 }
00057 
00058 DataManager::~DataManager()
00059 {
00060 }
00061 
00062 Server *DataManager :: getServer( const char *name )
00063 {
00064     QListIterator<Server> it( serverList );
00065     while ( it.current() && it.current()->getServerName() != name )
00066     {
00067                 ++it;
00068         }
00069 
00070         return it.current();
00071 }
00072 
00073 Destination *DataManager :: getDestination( const char *name )
00074 {
00075     QListIterator<Destination> it( destList );
00076     while ( it.current() && it.current()->getDestinationName() != name )
00077     {
00078                 ++it;
00079         }
00080 
00081         return it.current();
00082 }
00083 
00084 void DataManager :: loadServers()
00085 {
00086     // First add our local server - not really a server but
00087     // the local config (which packages are installed)
00088     serverList.append( new Server( LOCAL_SERVER, "" ) );
00089     serverList.append( new Server( LOCAL_IPKGS, "" ) );
00090 
00091 #ifdef QWS
00092     Config cfg( "aqpkg" );
00093     cfg.setGroup( "destinations" );
00094 #endif
00095 
00096     // Read file from /etc/ipkg.conf
00097     QString ipkg_conf = IPKG_CONF;
00098     FILE *fp;
00099     fp = fopen( ipkg_conf, "r" );
00100     char line[130];
00101     QString lineStr;
00102     if ( fp == NULL )
00103     {
00104         return;
00105     }
00106     else
00107     {
00108         while ( fgets( line, sizeof line, fp) != NULL )
00109         {
00110             lineStr = line;
00111             if ( lineStr.startsWith( "src" ) || lineStr.startsWith( "#src" ) || lineStr.startsWith( "# src" ) )
00112             {
00113                 char alias[20];
00114                 char url[100];
00115 
00116                 // Looks a little wierd but read up to the r of src (throwing it away),
00117                 // then read up to the next space and throw that away, the alias
00118                 // is next.
00119                 // Should Handle #src, # src, src, and combinations of
00120                 sscanf( lineStr, "%*[^r]%*[^ ] %s %s", alias, url );
00121                 Server *s = new Server( alias, url );
00122                 if ( lineStr.startsWith( "src" ) )
00123                     s->setActive( true );
00124                 else
00125                     s->setActive( false );
00126 
00127                 serverList.append( s );
00128 
00129             }
00130             else if ( lineStr.startsWith( "dest" ) )
00131             {
00132                 char alias[20];
00133                 char path[50];
00134                 sscanf( lineStr, "%*[^ ] %s %s", alias, path );
00135                 Destination *d = new Destination( alias, path );
00136                 bool linkToRoot = true;
00137 #ifdef QWS
00138                 QString key = alias;
00139                 key += "_linkToRoot";
00140                 linkToRoot = cfg.readBoolEntry( key, true );
00141 #endif
00142                 d->linkToRoot( linkToRoot );
00143 
00144                 destList.append( d );
00145             }
00146             else if ( lineStr.startsWith( "option" ) || lineStr.startsWith( "#option" ) )
00147             {
00148                 char type[20];
00149                 char val[100];
00150                 sscanf( lineStr, "%*[^ ] %s %s", type, val );
00151                 if ( qstricmp( type, "http_proxy" ) == 0 )
00152                 {
00153                     httpProxy = val;
00154                     if ( lineStr.startsWith( "#" ) )
00155                         httpProxyEnabled = false;
00156                     else
00157                         httpProxyEnabled = true;
00158                 }
00159                 if ( qstricmp( type, "ftp_proxy" ) == 0 )
00160                 {
00161                     ftpProxy = val;
00162                     if ( lineStr.startsWith( "#" ) )
00163                         ftpProxyEnabled = false;
00164                     else
00165                         ftpProxyEnabled = true;
00166                 }
00167                 if ( qstricmp( type, "proxy_username" ) == 0 )
00168                     proxyUsername = val;
00169                 if ( qstricmp( type, "proxy_password" ) == 0 )
00170                     proxyPassword = val;
00171             }
00172         }
00173     }
00174     fclose( fp );
00175 
00176     reloadServerData( );
00177 }
00178 
00179 void DataManager :: reloadServerData( )
00180 {
00181     emit progressSetSteps( serverList.count() );
00182     emit progressSetMessage( tr( "Reading configuration..." ) );
00183 
00184     QString serverName;
00185     int i = 0;
00186 
00187     Server *server;
00188     QListIterator<Server> it( serverList );
00189     for ( ; it.current(); ++it )
00190     {
00191         server = it.current();
00192         serverName = server->getServerName();
00193         i++;
00194         emit progressUpdate( i );
00195         qApp->processEvents();
00196 
00197         // Now we've read the config file in we need to read the servers
00198         // The local server is a special case. This holds the contents of the
00199         // status files the number of which depends on how many destinations
00200         // we've set up
00201         // The other servers files hold the contents of the server package list
00202         if ( serverName == LOCAL_SERVER )
00203                 server->readStatusFile( destList );
00204         else if ( serverName == LOCAL_IPKGS )
00205             server->readLocalIpks( getServer( LOCAL_SERVER ) );
00206         else
00207             server->readPackageFile( getServer( LOCAL_SERVER ) );
00208         }
00209 }
00210 
00211 void DataManager :: writeOutIpkgConf()
00212 {
00213     QFile f( IPKG_CONF );
00214     if ( !f.open( IO_WriteOnly ) )
00215     {
00216         return;
00217     }
00218 
00219     QTextStream t( &f );
00220 /*
00221     QString ipkg_conf = IPKG_CONF;
00222     ofstream out( ipkg_conf );
00223 */
00224     t << "# Written by AQPkg\n";
00225     t << "# Must have one or more source entries of the form:\n";
00226     t << "#\n";
00227     t << "#   src <src-name> <source-url>\n";
00228     t << "#\n";
00229     t << "# and one or more destination entries of the form:\n";
00230     t << "#\n";
00231     t << "#   dest <dest-name> <target-path>\n";
00232     t << "#\n";
00233     t << "# where <src-name> and <dest-names> are identifiers that\n";
00234     t << "# should match [a-zA-Z0-9._-]+, <source-url> should be a\n";
00235     t << "# URL that points to a directory containing a Familiar\n";
00236     t << "# Packages file, and <target-path> should be a directory\n";
00237     t << "# that exists on the target system.\n\n";
00238 
00239     // Write out servers
00240     Server *server;
00241     QListIterator<Server> it( serverList );
00242     while ( it.current() )
00243     {
00244         server = it.current();
00245         QString alias = server->getServerName();
00246         // Don't write out local as its a dummy
00247         if ( alias != LOCAL_SERVER && alias != LOCAL_IPKGS )
00248         {
00249             QString url = server->getServerUrl();;
00250 
00251             if ( !server->isServerActive() )
00252                 t << "#";
00253             t << "src " << alias << " " << url << endl;
00254         }
00255 
00256         ++it;
00257     }
00258 
00259     t << endl;
00260 
00261     // Write out destinations
00262     QListIterator<Destination> it2( destList );
00263     while ( it2.current() )
00264     {
00265         t << "dest " << it2.current()->getDestinationName() << " " << it2.current()->getDestinationPath() << endl;
00266         ++it2;
00267     }
00268 
00269     t << endl;
00270     t << "# Proxy Support\n";
00271 
00272     if ( !httpProxyEnabled && httpProxy == "" )
00273         t << "#option http_proxy http://proxy.tld:3128\n";
00274     else
00275     {
00276         if ( !httpProxyEnabled )
00277             t << "#";
00278         t << "option http_proxy " << httpProxy << endl;
00279     }
00280 
00281     if ( !ftpProxyEnabled && ftpProxy == "" )
00282         t << "#option ftp_proxy http://proxy.tld:3128\n";
00283     else
00284     {
00285         if ( !ftpProxyEnabled )
00286             t << "#";
00287         t << "option ftp_proxy " << ftpProxy << endl;
00288     }
00289     if ( proxyUsername == "" || (!httpProxyEnabled && !ftpProxyEnabled) )
00290         t << "#option proxy_username <username>\n";
00291     else
00292         t << "option proxy_username " << proxyUsername << endl;
00293     if ( proxyPassword == "" || (!httpProxyEnabled && !ftpProxyEnabled) )
00294         t << "#option proxy_password <password>\n\n";
00295     else
00296         t << "option proxy_password " << proxyPassword << endl<< endl;
00297 
00298     t << "# Offline mode (for use in constructing flash images offline)\n";
00299     t << "#option offline_root target\n";
00300 
00301     f.close();
00302 }
00303 
00304 
00305 void DataManager :: setAvailableCategories( QString section )
00306 {
00307     QString sectstr = "#";
00308     sectstr.append( section.lower() );
00309     sectstr.append( "#" );
00310     if ( availableCategories.find( sectstr ) == -1 )
00311     {
00312         availableCategories.append( section );
00313         availableCategories.append( "#" );
00314     }
00315 }

Generated on Sat Nov 5 16:17:46 2005 for OPIE by  doxygen 1.4.2