fixes
[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)
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!="" )
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                 iIconData = QByteArray::fromBase64( iIconData );
86                 if( !iIcon->loadFromData( iIconData ) ) {
87                         qDebug() << "Warning: Package" << iName << "has invalid icon data";
88                 }
89         }
90
91 }
92
93 bool Package::isUpgradeable()
94 {
95         if( iSection=="user/hidden" )
96                 return false;
97
98         if( iIsInstalled )
99         {
100                 QString newer = upgradeableVersion();
101                 //qDebug() << newer << iVersion << versionCompare(newer,iVersion);
102                 return versionCompare(newer,iVersion);
103         }
104
105         return false;
106 }
107
108 QString Package::upgradeableVersion()
109 {
110         QString ver="";
111         if( isInstalled() ) {
112                 Package* newer = iAptInterface->packagesAvailable()->value(iName,0);
113                 if( newer )
114                         ver = newer->version();
115         }
116         return ver;
117 }
118
119 Package* Package::availablePackage()
120 {
121         if( !isInstalled() )
122                 return 0;
123
124         Package* newer = iAptInterface->packagesAvailable()->value(iName,0);
125
126         return newer;
127 }
128
129 void Package::updateStatus()
130 {
131         if( iIsInstalled ) {
132                 iPkgStatus = PkgStatInstalled;
133                 if( isUpgradeable() )
134                         iPkgStatus = PkgStatUpgradeable;
135         } else {
136                 iPkgStatus = PkgStatNotInstalled;
137         }
138 }
139
140 Package::packageStatus Package::status()
141 {
142         updateStatus(); // not optimal, but it's here just in case
143         return iPkgStatus;
144 }
145
146 bool Package::hasIconData()
147 {
148         if( iIconData.length()>0 )
149                 return true;
150         else
151                 return false;
152 }
153
154 bool Package::versionCompare(QString isNewer, QString compare)
155 {
156         //int res=0;
157         int res = debVS.CmpVersion(isNewer.toStdString(), compare.toStdString());
158         //qDebug() << isNewer << compare << res;
159
160         if( res > 0 )
161                 return true;
162
163         return false;
164 }
165
166 QStringList Package::toTrimmedRichTextList(QList<QByteArray> list_in)
167 {
168         QStringList list_out;
169
170         for(int i=0; i<list_in.count(); i++)
171         {
172                 QString s = list_in.at(i).trimmed();
173                 s.replace('<',"&lt;");
174                 s.replace('>',"&gt;");
175                 list_out << s;
176         }
177
178         return list_out;
179 }
180
181 QStringList Package::checkConflicts_RichText()
182 {
183         QStringList list;
184
185         if( iConflicts.count() == 0 )
186                 return list;
187
188         for(int i=0; i<iConflicts.count(); i++)
189         {
190                 QString s = iConflicts.at(i).trimmed();
191                 QString name = s;
192                 QString ver = "";
193                 int pos = s.indexOf('(');
194                 bool confl = false;
195                 if( pos != -1 ) {
196                         name = s.left(pos).trimmed();
197                         ver = s.mid(pos).trimmed();
198                 }
199                 Package* p_inst = iAptInterface->packagesInstalled()->value(name,0);
200                 Package* p_avail = iAptInterface->packagesAvailable()->value(name,0);
201
202                 if( p_inst && p_inst->isInstalled() ) {
203                         confl = versionConflicts(ver, p_inst->version());
204                 }
205                 if( p_avail && p_avail->markedOperation()==Package::PkgOpInstallUpgrade ) {
206                         if( !confl )
207                                 confl = versionConflicts(ver, p_avail->version());
208                 }
209
210                 if( confl )
211                 {
212                         qDebug() << "package" << iName << "conflicts:" << name << ver;
213                         s.replace('<',"&lt;");
214                         s.replace('>',"&gt;");
215                         list << s;
216                 }
217         }
218
219         return list;
220 }
221
222 bool Package::versionConflicts(QString conflictVer, QString operVer)
223 {
224         if( conflictVer=="" )
225                 return true;
226
227         bool confl = false;
228         conflictVer.remove('(');
229         conflictVer.remove(')');
230
231         if( conflictVer.startsWith("<<") ) {
232                 conflictVer = conflictVer.mid(2).trimmed();
233                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
234                 if( res < 0 )
235                         confl = true;
236         } else if( conflictVer.startsWith("<=") ) {
237                 conflictVer = conflictVer.mid(2).trimmed();
238                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
239                 if( res <= 0 )
240                         confl = true;
241         } else if( conflictVer.startsWith("=") ) {
242                 conflictVer = conflictVer.mid(1).trimmed();
243                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
244                 if( res == 0 )
245                         confl = true;
246         } else if( conflictVer.startsWith(">=") ) {
247                 conflictVer = conflictVer.mid(2).trimmed();
248                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
249                 if( res >= 0 )
250                         confl = true;
251         } else if( conflictVer.startsWith(">>") ) {
252                 conflictVer = conflictVer.mid(2).trimmed();
253                 int res = debVS.CmpVersion(conflictVer.toStdString(), operVer.toStdString());
254                 if( res > 0 )
255                         confl = true;
256         }
257
258         return confl;
259 }