summaryrefslogtreecommitdiff
path: root/noncore/unsupported/oipkg/packagelist.cpp
blob: 6f0b56fea9799331f191430afa3aa1d5d43ecc6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
// (c) 2002 Patrick S. Vogt <tille@handhelds.org>
#include "packagelist.h"

#include <assert.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qtextstream.h>

#include "debug.h"

static QDict<OipkgPackage>  *packageListAll;
static int packageListAllRefCount = 0;

PackageList::PackageList(QObject *parent, const char *name)
  : QObject(parent,name), packageIter( packageList )
{
  empty=true;
  if (!packageListAll) packageListAll = new QDict<OipkgPackage>();
  packageListAllRefCount++;
  sections << "All";
  subSections.insert("All", new QStringList() );
  QStringList *ss = subSections["All"];
  *ss << "All";
  aktSection = "All";
  aktSubSection = "All";
}

PackageList::PackageList( PackageManagerSettings* s, QObject *parent, const char *name)
  : QObject(parent,name), packageIter( packageList )
{
  settings = s;
  PackageList(parent, name);
}

PackageList::~PackageList()
{
	if (--packageListAllRefCount < 1 ) delete packageListAll;
}

/** Inserts a package into the list */
void PackageList::insertPackage( OipkgPackage* pack )
{
	if (!pack) return;
  OipkgPackage* p = packageListAll->find( pack->name() );
  if ( p )
    {
      if ( (p->version() == pack->version())
//      		&& (p->dest() == pack->dest())
        	)
      {
	      p->copyValues( pack );
  	    delete pack;
    	  pack = p;
      } else {
      	QDict<OipkgPackage> *packver = p->getOtherVersions();
//       	p->setName( pack->name()+"["+p->version()+"]" );
				if (!packver)
    		{
    				packver = new QDict<OipkgPackage>();    	
		       	packver->insert( pack->name(), p );
          	p->setOtherVersions( packver );
        }
       	pack->setName( pack->name() );//+"["+pack->version()+"]" );
       	pack->setOtherVersions( packver );
       	packver->insert( pack->name(), pack );
				packageListAll->insert( pack->name(), pack );
				packageList.insert( pack->name(), pack );
      	origPackageList.insert( pack->name(), pack );
      }
    }else{
      packageListAll->insert( pack->name(), pack );
      packageList.insert( pack->name(), pack );
      origPackageList.insert( pack->name(), pack );
    };
  empty=false;
  updateSections( pack );
}

void PackageList::filterPackages( QString f )
{	
  packageList.clear();
  QDictIterator<OipkgPackage> filterIter( origPackageList );
  filterIter.toFirst();
  OipkgPackage *pack= filterIter.current() ;
  while ( pack )
    {	
      if (
      			((aktSection=="All")||(pack->section()==aktSection)) &&
	   				((aktSubSection=="All")||(pack->subSection()==aktSubSection)) &&
        		 pack->name().contains( f )
    			)
			{
	  		packageList.insert( pack->name(), pack );
			}
      ++filterIter;
      pack = filterIter.current();
    }
}

OipkgPackage* PackageList::find( QString n )
{
  return packageList.find( n );
}

OipkgPackage* PackageList::first()
{ 	
  packageIter.toFirst();
  return packageIter.current();	
}

OipkgPackage* PackageList::next()
{
  ++packageIter;
  return packageIter.current();
}

QStringList PackageList::getSections()
{
  sections.sort();
  return sections;
}

QStringList PackageList::getSubSections()
{
  QStringList ss;
  if ( !subSections[aktSection] ) return ss;
  ss = *subSections[aktSection];
  ss.sort();
  return ss;
}

void PackageList::setSection( QString sec )
{
  aktSection = sec;
}

void PackageList::setSubSection( QString ssec )
{
  aktSubSection = ssec;
}

void PackageList::updateSections( OipkgPackage* pack )
{
  QString s = pack->section();
  if ( s.isEmpty() || s == "") return;
  if ( !sections.contains(s) ) sections += s;
  QString ss = pack->subSection();
  if ( ss.isEmpty() || ss == "" ) return;
  if ( !subSections[s] ) {
    subSections.insert( s, new QStringList() );
    QStringList *subsecs = subSections[s];
    *subsecs += "All";
  }
  QStringList *subsecs = subSections[s];
  if ( !subsecs->contains(ss) ) *subsecs += ss;
//  if ( !subSections["All"] ) subSections.insert( "All", new QStringList() );
//  subsecs = subSections["All"];
//  *subsecs += ss;
}


void PackageList::readFileEntries( QString filename, QString dest )
{ 	
	pvDebug(5,"PackageList::readFileEntries "+filename+" dest "+dest);
  QStringList packEntry;
  QFile f( filename );
  if ( !f.open(IO_ReadOnly) ) return;
  QTextStream *statusStream = new QTextStream( &f );
  while ( !statusStream ->eof() )
  {
     QString line = statusStream->readLine();
     if ( line.find(QRegExp("[\n\t ]*")) || line == "" )
			{
			  //end of package
	  		if ( ! packEntry.isEmpty() )
		    {
		      OipkgPackage *p = new OipkgPackage( packEntry, settings );
	    	  if ( p )
					{
			      p->setDest( dest );
		  			insertPackage( p );
					  packEntry.clear();
					}
	   		 }
			}else{
	  		packEntry << line;
			};
    }
  //there might be no nl at the end of the package file
	if ( ! packEntry.isEmpty() )
	{
		OipkgPackage *p = new OipkgPackage( packEntry, settings );
	  if ( p )
		{
	      p->setDest( dest );
	 			insertPackage( p );
			  packEntry.clear();
		}
	}
 	delete statusStream;
	f.close();
  return;
}

void PackageList::setSettings( PackageManagerSettings *s )
{
  settings = s;
}

OipkgPackage* PackageList::getByName( QString n )
{
	return origPackageList[n];
}

void PackageList::clear()
{
	origPackageList.clear();
	packageList.clear();
}

void PackageList::allPackages()
{
	packageList.clear();
  QDictIterator<OipkgPackage> filterIter( origPackageList );
  filterIter.toFirst();
  OipkgPackage *pack= filterIter.current() ;
  while ( pack )
    {	
	  	packageList.insert( pack->name(), pack );
      ++filterIter;
      pack = filterIter.current();
    }
}