summaryrefslogtreecommitdiff
path: root/noncore/unsupported/oipkg/pmipkg.cpp
blob: b2ddf9550556dc714a54d458a55471bf445423d6 (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
242
243
244
245
246
247
#include "pmipkg.h"
#include "pkdesc.h"
#include "pkfind.h"
#include "pksettings.h"
#include "package.h"
#include "packagelistitem.h"

#include <qpe/process.h>
#include <qpe/resource.h>
#include <qpe/config.h>
#include <qpe/stringutil.h>
#include <qpe/qcopenvelope_qws.h>
#include <qdir.h>
#include <qfile.h>
#include <qmultilineedit.h>
#include <qstring.h>
#include <qtextstream.h>
#include <qtextview.h>

#include <stdlib.h>
#include <unistd.h>

#include "mainwindow.h"
#include "runwindow.h"


#define PARSE_FILELIST
// #define IPKG_FILELIST

PmIpkg::PmIpkg( PackageManagerSettings* s, QWidget* p,  const char * name, WFlags f )
//  : RunWindow ( p, name, f )
  	: QObject ( p )
{
  settings = s;
  runwindow = new RunWindow ( p, name, f );
  linkDest = new QCopChannel( "QPE/oipkg", this );
  connect( linkDest, SIGNAL(received(const QCString &, const QByteArray &)),
	   this, SLOT(linkDestination( const QString &, const  QByteArray&)) );
}

PmIpkg::~PmIpkg()
{
}

//#define PROC
#define SYSTEM
int PmIpkg::runIpkg(const QString& args)
{
  pvDebug(2,"PmIpkg::runIpkg "+args);
#ifdef PROC
  QStringList cmd = "/usr/bin/ipkg ";
#endif
#ifdef SYSTEM
  QString cmd = "/usr/bin/ipkg ";
#endif
  cmd += " -dest "+settings->getDestinationName();
 	cmd += " -force-defaults ";

  out( "<hr><br>Starting to "+ args+"<br>\n");
  cmd += args;
  out( "running:<br>\n"+cmd+"<br>\n" );
  int r = 0;
#ifdef PROC
	QString o = "start";
  Process ipkg( cmd );
  QString description;
  ipkg.exec("",o);
  out( o );
#endif
#ifdef SYSTEM
  QString redirect = "/tmp/oipkg.pipe";
  cmd += " | tee "+redirect+" 2>&1";
  pvDebug(2, "running >"+cmd+"<");
	r = system(cmd.latin1());
  QFile f( redirect );
  while ( ! f.open(IO_ReadOnly) ) {};
 // if ( f.open(IO_ReadOnly) ) {};
  {
    QTextStream t( &f );
    QString fp;
    while ( !t.eof() )
    {
				out( t.readLine() +"<br>" );
    }
  }
  f.close();
  out( "Finished!<br>");
#endif

  return r;
}

void PmIpkg::makeLinks(QString pack)
{
	pvDebug( 2, "PmIpkg::makeLinks "+ pack);
  out( "<br>creating links<br>" );
  QString dest = settings->getDestinationUrl();
  out("for package "+pack+" in "+dest+"<br>");
#ifdef IPKG_FILELIST
  system(("ipkg -d "+dest+" files "+pack+"> /tmp/oipkg.pipe 2>&1").latin1());
  QFile f( "/tmp/oipkg.pipe" );
  while ( ! f.open(IO_ReadOnly) ) {};
#endif
#ifdef PARSE_FILELIST
	{
    Config cfg( "oipkg", Config::User );
    cfg.setGroup( "Common" );
    QString statusDir = cfg.readEntry( "statusDir", "" );
	}
 	QString fn = dest+"/"+statusDir+"/info/"+pack+".list";
  QFile f( fn );
  if ( ! f.open(IO_ReadOnly) )
  {
  	out( "<b>Panik!</b> Could not open:<br>"+fn );
  };
#endif
  QTextStream t( &f );
  QString fp;
  while ( !t.eof() )
    {
      fp = t.readLine();
      processLinkDir( fp, dest );
    }	
  f.close();
}

void PmIpkg::processLinkDir( QString file, QString dest )
{

#ifdef PARSE_FILELIST
  QString destFile = file;
	file = dest+"/"+file;
#endif
#ifdef IPKG_FILELIST
  QString destFile = file.right( file.length() - dest.length() );
#endif
  QFileInfo fileInfo( file );
  if ( fileInfo.isDir() )
  {
  	QDir destDir( destFile );
    destDir.mkdir( destFile, true );
    QDir d( file );
    d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
    const QFileInfoList *list = d.entryInfoList();
    QFileInfoListIterator it( *list );
    QFileInfo *fi;
    while ( (fi=it.current()) )
    {
      out( "<b>"+fi->absFilePath()+"</b>" );
  		processLinkDir( fi->absFilePath(), dest );      			
			++it;
    }
  } else
  if ( fileInfo.isFile() )
  {
    const char *instFile = strdup( (file).ascii() );
    const char *linkFile = strdup(  (destFile).ascii());
    out( "linking: "+file+" -> "+destFile  );
    qDebug( "linking: %s -> %s", instFile, linkFile );
    symlink( instFile, linkFile );
  }
}

void PmIpkg::commit( PackageList pl )
{   	
  runwindow->showMaximized();
//  exec();
  runwindow->outPut->setText("");
  out( "<h1>Starting...</h1><br>\n");
  QStringList to_remove, to_install;   	

  QString rem="<b>To remove:</b><br>\n";
  QString inst="<b>To install:</b><br>\n";;
  for( Package *pack = pl.first();pack ; (pack = pl.next())  )
    {
      if ( pack && (pack->name() != "") && pack)
	{
	  if ( pack->toInstall() )
	    {
	      to_install.append( pack->name() );
	      inst += pack->name()+"<br>";
	    }
	  if ( pack->toRemove() )
	    {
	      to_remove.append( pack->name() );
	      rem += pack->name()+"<br>";
	    }
	}
    }

  out("<p>"+inst+"</p>"+"<p>"+rem+"</p>");
  bool ok=TRUE;

  qDebug("to remove=%i; to install=%i",to_remove.count(),to_install.count());

  int jobs = to_remove.count()+to_install.count();
  if ( jobs < 1 ) return;

  if ( to_remove.count() )
    for (QStringList::ConstIterator it=to_remove.begin(); it!=to_remove.end(); ++it)
      if ( runIpkg("remove " + *it) != 0 ) ok = false;
  if ( to_install.count() )
    for (QStringList::ConstIterator it=to_install.begin(); it!=to_install.end(); ++it)
    {
      if ( runIpkg("install " + *it) == 0 )
		  {    	
	    	if ( settings->createLinks() )
					makeLinks( *it );
  		}else{
      	ok = false;
      }
    }
	
  // ##### If we looked in the list of files, we could send out accurate
  // ##### messages. But we don't bother yet, and just do an "all".
  QCopEnvelope e("QPE/System", "linkChanged(QString)");
  QString lf = QString::null;
  e << lf;
}

void PmIpkg::linkDestination( const QString msg, const QByteArray dest )
// add 3rd package parameter
{
	qDebug("msg="+msg+" -- "+QString(dest) );	
//  QDir d( src );
//  d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
////  if (! d.exists() ) return;
//  const QFileInfoList *list = d.entryInfoList();
//  QFileInfoListIterator it( *list );
//  QFileInfo *fi;
//  while ( (fi=it.current()) ) {
//    processLinkDir( fi->absFilePath(), dest );      			
//    ++it;
//  }
}

void PmIpkg::update()
{
  runwindow->showMaximized();
	runIpkg( "update" );
}

void PmIpkg::out( QString o )
{
	runwindow->outPut->setText( runwindow->outPut->text()+o );
  runwindow->outPut->setContentsPos(0,runwindow->outPut->contentsHeight());
}