0.7.1
[fapman] / package.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 "package.h"
22 #include "repository.h"
23 #include "aaptinterface.h"
24 #include "blacklistselect.h"
25
26 #include "apt-src/debversion.h"
27
28
29 Package::Package(QByteArray name_, AAptInterface *apt_):
30                 iAptInterface(apt_), iName(name_), iIsInstalled(false), iMarkedForOperation(false),
31                 iPkgStatus(PkgStatUnknown), iSize(0), iInstalledSize(0), iMarkedOperation(PkgOpNone),
32                 iBlacklist(BlacklistSelect::BlacklistNone), iIcon(0), iPinned(false)
33 {
34 }
35
36 Package::~Package()
37 {
38         if( iIcon ) {
39                 delete iIcon;
40                 iIcon=0;
41         }
42 }
43
44 QString Package::displayName() const
45 {
46         QString pkgname = name();
47         if( !iMaemoDisplayName.isEmpty() )
48                 pkgname = maemoDisplayName();
49         QString n( pkgname.at(0) );
50         n = n.toUpper();
51         pkgname.replace(0,1,n);
52         return pkgname;
53 }
54
55 QString Package::fileName() const
56 {
57         if( iFullFileNames.count()>0 ) {
58                 return iFullFileNames.at(0).mid( iFullFileNames.at(0).lastIndexOf('/')+1 );
59         } else {
60                 qDebug() << "Warning: package has no file name information";
61                 return "unknown_filename";
62         }
63 }
64
65 void Package::setMarkedForOperation(operation op_)
66 {
67         if( iMarkedOperation != op_ ) {
68                 if( op_==PkgOpNone ) {
69                         if( iAptInterface )
70                                 iAptInterface->setNumSelectedPackages( iAptInterface->numSelectedPackages()-1 );
71                 } else if( iMarkedOperation==PkgOpNone ) {
72                         if( iAptInterface )
73                                 iAptInterface->setNumSelectedPackages( iAptInterface->numSelectedPackages()+1 );
74                 }
75         }
76
77         iMarkedOperation = op_;
78
79         if( op_ == PkgOpNone )
80                 iMarkedForOperation = false;
81         else
82                 iMarkedForOperation = true;
83 }
84
85 void Package::convertIcon()
86 {
87         if( iIconData.length() > 0 && iIcon == 0 ) {
88                 iIcon = new QPixmap();
89                 if( !iIcon->loadFromData(QByteArray::fromBase64(iIconData)) ) {
90                         qDebug() << "Warning: Package" << iName << "has invalid icon data";
91                 }
92         }
93 }
94
95 bool Package::isUpgradeable() const
96 {
97         if( iSection=="user/hidden" || iPinned )
98                 return false;
99
100         if( iIsInstalled )
101         {
102                 QString newer = upgradeableVersion();
103                 //qDebug() << newer << iVersion << versionCompare(newer,iVersion);
104                 return versionCompare(newer,iVersion);
105         }
106
107         return false;
108 }
109
110 QString Package::upgradeableVersion() const
111 {
112         QString ver;
113         if( isInstalled() ) {
114                 Package* newer = iAptInterface->packagesAvailable()->value(iName,0);
115                 if( newer )
116                         ver = newer->version();
117         }
118         return ver;
119 }
120
121 Package* Package::availablePackage() const
122 {
123         if( !isInstalled() )
124                 return 0;
125
126         Package* newer = iAptInterface->packagesAvailable()->value(iName,0);
127
128         return newer;
129 }
130
131 void Package::updateStatus()
132 {
133         if( iIsInstalled ) {
134                 iPkgStatus = PkgStatInstalled;
135                 if( isUpgradeable() )
136                         iPkgStatus = PkgStatUpgradeable;
137         } else {
138                 iPkgStatus = PkgStatNotInstalled;
139         }
140 }
141
142 Package::packageStatus Package::status()
143 {
144         updateStatus(); // just in case
145         return iPkgStatus;
146 }
147
148 bool Package::hasIconData() const
149 {
150         if( iIconData.length()>0 )
151                 return true;
152         else
153                 return false;
154 }
155
156 bool Package::versionCompare(QString isNewer, QString compare)
157 {
158         //int res=0;
159         int res = debVS.CmpVersion(isNewer.toStdString(), compare.toStdString());
160         //qDebug() << isNewer << compare << res;
161
162         if( res > 0 )
163                 return true;
164
165         return false;
166 }
167
168 QStringList Package::toTrimmedRichTextList(QList<QByteArray> list_in)
169 {
170         QStringList list_out;
171
172         for(int i=0; i<list_in.count(); i++)
173         {
174                 QString s = list_in.at(i).trimmed();
175                 s.replace('<',"&lt;");
176                 s.replace('>',"&gt;");
177                 list_out << s;
178         }
179
180         return list_out;
181 }
182
183 QStringList Package::checkConflicts_RichText() const
184 {
185         QStringList list;
186
187         if( iConflicts.count() == 0 )
188                 return list;
189
190         for(int i=0; i<iConflicts.count(); i++)
191         {
192                 QString s = iConflicts.at(i).trimmed();
193                 QString name = s;
194                 QString ver = "";
195                 int pos = s.indexOf('(');
196                 bool confl = false;
197                 if( pos != -1 ) {
198                         name = s.left(pos).trimmed();
199                         ver = s.mid(pos).trimmed();
200                 }
201                 Package* p_inst = iAptInterface->packagesInstalled()->value(name,0);
202                 Package* p_avail = iAptInterface->packagesAvailable()->value(name,0);
203
204                 if( p_inst && p_inst->isInstalled() ) {
205                         confl = versionConflicts(ver, p_inst->version());
206                 }
207                 if( p_avail && p_avail->markedOperation()==Package::PkgOpInstallUpgrade ) {
208                         if( !confl )
209                                 confl = versionConflicts(ver, p_avail->version());
210                 }
211
212                 if( confl )
213                 {
214                         qDebug() << "package" << iName << "conflicts:" << name << ver;
215                         s.replace('<',"&lt;");
216                         s.replace('>',"&gt;");
217                         list << s;
218                 }
219         }
220
221         return list;
222 }
223
224 bool Package::versionConflicts(QString conflictVer, QString operVer)
225 {
226         if( conflictVer.isEmpty() )
227                 return true;
228
229         bool confl = false;
230         conflictVer.remove('(');
231         conflictVer.remove(')');
232
233         if( conflictVer.startsWith("<<") ) {
234                 conflictVer = conflictVer.mid(2).trimmed();
235                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
236                 if( res < 0 )
237                         confl = true;
238         } else if( conflictVer.startsWith("<=") ) {
239                 conflictVer = conflictVer.mid(2).trimmed();
240                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
241                 if( res <= 0 )
242                         confl = true;
243         } else if( conflictVer.startsWith("=") ) {
244                 conflictVer = conflictVer.mid(1).trimmed();
245                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
246                 if( res == 0 )
247                         confl = true;
248         } else if( conflictVer.startsWith(">=") ) {
249                 conflictVer = conflictVer.mid(2).trimmed();
250                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
251                 if( res >= 0 )
252                         confl = true;
253         } else if( conflictVer.startsWith(">>") ) {
254                 conflictVer = conflictVer.mid(2).trimmed();
255                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
256                 if( res > 0 )
257                         confl = true;
258         }
259
260         return confl;
261 }
262
263 QString Package::maintainerRichText() const
264 {
265         QString m = iMaintainer;
266         m.replace('<',"&lt;");
267         m.replace('>',"&gt;");
268         return m;
269 }