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