release 0.6.6
[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()
26 {
27         iEnabled = false;
28 }
29
30 QString Repository::toString()
31 {
32         QString str;
33         if( iEnabled )
34                 str = "deb ";
35         else
36                 str = "#deb ";
37
38         str += iUrl + iDir + " " + iDist;
39         if( !iComponents.isEmpty() )
40                 str += " " + iComponents;
41
42         return( str.trimmed() );
43 }
44
45 QStringList Repository::toListFileNames()
46 {
47         QStringList names;
48
49         QStringList com = iComponents.split(' ');
50         QString disttext;
51         if( com.count()==0 )
52                 com << "";
53         for( int i=0; i<com.count(); i++ ) {
54                 QString chopUrl = iUrl.mid( iUrl.indexOf("://")+3 );
55                 if( !com.at(i).isEmpty() )
56                         disttext = "dists_";
57                 else
58                         disttext = "_";
59                 QString str = chopUrl + iDir + disttext + iDist + "_" + com.at(i);
60                 if( !com.at(i).isEmpty() )
61                         str += "_binary-armel_Packages";
62                 else
63                         str += "_Packages";
64                 str.replace('/','_');
65                 str.replace("___","_");
66                 str.replace("__","_");
67                 names << str;
68         }
69
70         return names;
71 }
72
73 bool Repository::setFromString(QString repo)
74 {
75         QStringList parts = repo.trimmed().split(' ');
76         if( parts.count() < 3 )
77                 return false;
78
79         if( parts.at(0) == "deb" ) {
80                 iEnabled=true;
81         } else if( parts.at(0) == "#deb" ) {
82                 iEnabled=false;
83         } else {
84                 return false;
85         }
86
87         if( parts.at(1).indexOf("://") == -1 )
88                 return false;
89         int d = parts.at(1).indexOf('/', parts.at(1).indexOf("://")+3 );
90         if( d==-1 ) {
91                 iUrl = parts.at(1);
92                 iDir = "";
93         } else {
94                 iUrl = parts.at(1).left(d+1);
95                 iDir = parts.at(1).right( parts.at(1).length()-d-1 );
96         }
97
98         if( !parts.at(2).isEmpty() )
99                 iDist = parts.at(2);
100         else
101                 return false;
102
103         iComponents="";
104         if( parts.count()>3 ) {
105                 for(int i=3; i<parts.count(); i++)
106                         iComponents += parts.at(i) + " ";
107                 iComponents = iComponents.trimmed();
108         }
109
110         if( iDir != "" ) {
111                 iName = iDir;
112                 iName = iName.replace('/',' ').trimmed();
113         } else if( iDist != "" ) {
114                 iName = iDist;
115                 iName = iName.replace('/',' ').trimmed();
116         } else {
117                 iName = "Unknown";
118         }
119
120         /*
121         qDebug() << repo << "\n"
122                         << iName << "\n"
123                         << iUrl << "\n"
124                         << iDir << "\n"
125                         << iDist << "\n"
126                         << iComponents << "\n"
127                         << iEnabled << "\n"
128                         << toString() << "\n"; */
129
130         set( iName, iUrl, iDir, iDist, iComponents, iEnabled );  // performs further sanity checks
131
132         return true;
133 }
134
135 void Repository::set(QString name, QString url_dir, QString dist, QString components, bool enabled)
136 {
137         QString url;
138         QString dir;
139
140         if( url_dir.indexOf("://") == -1 )
141                 return;
142         int d = url_dir.indexOf('/', url_dir.indexOf("://")+3 );
143         if( d==-1 ) {
144                 url = url_dir;
145                 dir = "";
146         } else {
147                 url = url_dir.left(d+1);
148                 dir = url_dir.right( url_dir.length()-d-1 );
149         }
150
151         set( name, url, dir, dist, components, enabled );
152 }
153
154 void Repository::set(QString name, QString url, QString dir, QString dist, QString components, bool enabled)
155 {
156         iName = name.trimmed();
157         iUrl = url.trimmed();
158         if( !iUrl.contains("://") )
159                 iUrl.prepend("http://");
160         if( !iUrl.endsWith('/') )
161                 iUrl.append('/');
162         iDir = dir.trimmed();
163         if( !iDir.endsWith('/') && !iDir.isEmpty() )
164                 iDir.append('/');
165         iDist = dist.trimmed();
166         iComponents = components.trimmed();
167         iEnabled = enabled;
168
169         if( iDist.isEmpty() )
170                 iDist = ".";
171
172         // ensure that the protocol part of the URL is lowercase
173         int pos = iUrl.indexOf("://");
174         QString proto = iUrl.left(pos);
175         iUrl = proto.toLower() + iUrl.mid(pos);
176 }