0.7.1
[fapman] / repository.cpp
1 /*
2         This file is part of Faster Application Manager.
3
4         Faster Application Manager is free software: you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation, either version 3 of the License, or
7         (at your option) any later version.
8
9         Faster Application Manager is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with Faster Application Manager.  If not, see <http://www.gnu.org/licenses/>.
16
17         (C) Heikki Holstila 2010
18 */
19
20 #include <QtGui>
21 #include "repository.h"
22 #include "package.h"
23 #include "aaptinterface.h"
24
25 Repository::Repository(): iEnabled(false)
26 {
27 }
28
29 Repository::~Repository()
30 {
31 }
32
33 QString Repository::toString(bool noDisable) const
34 {
35         QString str;
36     if( iEnabled || noDisable )
37                 str = "deb ";
38         else
39                 str = "#deb ";
40
41         str += iUrl + iDir + " " + iDist;
42         if( !iComponents.isEmpty() )
43                 str += " " + iComponents;
44
45         return( str.trimmed() );
46 }
47
48 QStringList Repository::toListFileNames() const
49 {
50         QStringList names;
51
52         QStringList com = iComponents.split(' ');
53         QString disttext;
54         if( com.count()==0 )
55                 com << "";
56         for( int i=0; i<com.count(); i++ ) {
57                 QString chopUrl = iUrl.mid( iUrl.indexOf("://")+3 );
58                 if( !com.at(i).isEmpty() )
59                         disttext = "dists_";
60                 else
61                         disttext = "_";
62                 QString str = chopUrl + iDir + disttext + iDist + "_" + com.at(i);
63                 if( !com.at(i).isEmpty() )
64                         str += "_binary-armel_Packages";
65                 else
66                         str += "_Packages";
67                 str.replace('/','_');
68                 str.replace("___","_");
69                 str.replace("__","_");
70                 names << str;
71         }
72
73         return names;
74 }
75
76 bool Repository::setFromString(QString repo)
77 {
78         QStringList parts = repo.trimmed().split(' ');
79         if( parts.count() < 3 )
80                 return false;
81
82         if( parts.at(0) == "deb" ) {
83                 iEnabled=true;
84         } else if( parts.at(0) == "#deb" ) {
85                 iEnabled=false;
86         } else {
87                 return false;
88         }
89
90         if( parts.at(1).indexOf("://") == -1 )
91                 return false;
92         int d = parts.at(1).indexOf('/', parts.at(1).indexOf("://")+3 );
93         if( d==-1 ) {
94                 iUrl = parts.at(1);
95                 iDir = "";
96         } else {
97                 iUrl = parts.at(1).left(d+1);
98                 iDir = parts.at(1).right( parts.at(1).length()-d-1 );
99         }
100
101         if( !parts.at(2).isEmpty() )
102                 iDist = parts.at(2);
103         else
104                 return false;
105
106         iComponents="";
107         if( parts.count()>3 ) {
108                 for(int i=3; i<parts.count(); i++)
109                         iComponents += parts.at(i) + " ";
110                 iComponents = iComponents.trimmed();
111         }
112
113         if( iDir != "" ) {
114                 iName = iDir;
115                 iName = iName.replace('/',' ').trimmed();
116         } else if( iDist != "" ) {
117                 iName = iDist;
118                 iName = iName.replace('/',' ').trimmed();
119         } else {
120                 iName = "Unknown";
121         }
122
123         /*
124         qDebug() << repo << "\n"
125                         << iName << "\n"
126                         << iUrl << "\n"
127                         << iDir << "\n"
128                         << iDist << "\n"
129                         << iComponents << "\n"
130                         << iEnabled << "\n"
131                         << toString() << "\n"; */
132
133         set( iName, iUrl, iDir, iDist, iComponents, iEnabled );  // performs further sanity checks
134
135         return true;
136 }
137
138 void Repository::setUrlDir(QString url_dir)
139 {
140     QString url;
141     QString dir;
142
143     if( url_dir.indexOf("://") == -1 )
144         return;
145     int d = url_dir.indexOf('/', url_dir.indexOf("://")+3 );
146     if( d==-1 ) {
147         url = url_dir;
148         dir = "";
149     } else {
150         url = url_dir.left(d+1);
151         dir = url_dir.right( url_dir.length()-d-1 );
152     }
153
154     iUrl = url;
155     iDir = dir;
156 }
157
158 void Repository::set(QString name, QString url_dir, QString dist, QString components, bool enabled)
159 {
160         QString url;
161         QString dir;
162
163         if( url_dir.indexOf("://") == -1 )
164                 return;
165         int d = url_dir.indexOf('/', url_dir.indexOf("://")+3 );
166         if( d==-1 ) {
167                 url = url_dir;
168                 dir = "";
169         } else {
170                 url = url_dir.left(d+1);
171                 dir = url_dir.right( url_dir.length()-d-1 );
172         }
173
174         set( name, url, dir, dist, components, enabled );
175 }
176
177 void Repository::set(QString name, QString url, QString dir, QString dist, QString components, bool enabled)
178 {
179         iName = name.trimmed();
180         iUrl = url.trimmed();
181         if( !iUrl.contains("://") )
182                 iUrl.prepend("http://");
183         if( !iUrl.endsWith('/') )
184                 iUrl.append('/');
185         iDir = dir.trimmed();
186         if( !iDir.endsWith('/') && !iDir.isEmpty() )
187                 iDir.append('/');
188         iDist = dist.trimmed();
189         iComponents = components.trimmed();
190         iEnabled = enabled;
191
192         if( iDist.isEmpty() )
193                 iDist = ".";
194
195         // ensure that the protocol part of the URL is lowercase
196         int pos = iUrl.indexOf("://");
197         QString proto = iUrl.left(pos);
198         iUrl = proto.toLower() + iUrl.mid(pos);
199 }