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