0.7.1
[fapman] / installfile.cpp
1 #include <QtCore>
2 #include "installfile.h"
3 #include "repository.h"
4
5
6 InstallFile::InstallFile(QString filename): iIsValid(true)
7 {
8     sections sect = sectNone;
9     QStringList catalogues;
10     Repository* newrepo = 0;
11     QString currentRepoSect;
12
13     qDebug() << "--reading install file" << filename;
14
15     QFile f(filename);
16     if( f.open(QIODevice::ReadOnly | QIODevice::Text) )
17     {
18         while( !f.atEnd() && iIsValid )
19         {
20             QString line = f.readLine().simplified();
21             //qDebug() << ">" << line;
22
23             if( line == "[install]" ) {
24                 sect = sectInstall;
25                 qDebug() << "install section";
26             } else if( line == "[catalogues]" ) {
27                 sect = sectCatalogues;
28                 qDebug() << "catalogues section";
29             } else if( line.startsWith('[') && line.endsWith(']') ) {
30                 sect = sectRepo;
31                 currentRepoSect = line.mid(1,line.length()-2);
32                 if( !catalogues.contains( currentRepoSect ) )
33                 {
34                     iIsValid = false;
35                     iErrorString = "File contains an unknown section";
36                     sect = sectInvalid;
37                     qDebug() << "unknown section" << currentRepoSect;
38                 } else {
39                     qDebug() << "repo section" << currentRepoSect;
40                     newrepo = new Repository();
41                     if( !newrepo ) {
42                         qDebug() << "FATAL ERROR";
43                         iIsValid = false;
44                         return;
45                     }
46                     iRepos.append(newrepo);
47                 }
48             }
49
50             if( sect == sectCatalogues || sect == sectInstall ) {
51                 if( line.startsWith("catalogues") )
52                 {
53                     catalogues.append( TrimList( line.mid( line.indexOf('=')+1 ).split(';') ) );
54                     qDebug() << catalogues;
55                 }
56             }
57             if( sect == sectInstall ) {
58                 if( line.startsWith("package") ) {
59                     iInstallPackages.append( TrimList( line.mid( line.indexOf('=')+1 ).split(';') ) );
60                     qDebug() << iInstallPackages;
61                 }
62             }
63             if( sect == sectRepo ) {
64                 if( line.startsWith("name") ) {
65                     newrepo->setName( line.mid( line.indexOf('=')+1 ).trimmed() );
66                 }
67                 if( line.startsWith("uri") ) {
68                     newrepo->setEnabled(true);
69                     newrepo->setUrlDir( line.mid( line.indexOf('=')+1 ).trimmed() );
70                 }
71                 if( line.startsWith("components") ) {
72                     newrepo->setComponents( line.mid( line.indexOf('=')+1 ).trimmed() );
73                 }
74                 if( line.startsWith("dist") ) {
75                     newrepo->setDist( line.mid( line.indexOf('=')+1 ).trimmed() );
76                 }
77             }
78
79         }
80         f.close();
81     } else {
82         iIsValid = false;
83         iErrorString = "Could not open file";
84         qDebug() << "could not open file";
85     }
86
87     qDebug() << "--done" << filename;
88
89     for(int i=0; i<iRepos.count(); i++) {
90         if( iRepos.at(i) ) {
91             if( iRepos.at(i)->dist().isEmpty() )
92                 iRepos.at(i)->setDist("fremantle-1.3");  // this is not exactly the right way...
93         }
94     }
95 }
96
97 InstallFile::~InstallFile()
98 {
99     for(int i=0; i<iRepos.count(); i++) {
100         if( iRepos.at(i) ) {
101             delete iRepos.at(i);
102         }
103     }
104 }
105
106 QStringList InstallFile::TrimList(QStringList l)
107 {
108     QStringList n;
109     for(int i=0; i<l.count(); i++)
110     {
111         n << l.at(i).trimmed();
112     }
113     return n;
114 }