summaryrefslogtreecommitdiff
path: root/noncore/unsupported/oipkg/pmipkg.cpp
blob: 528365ec0b32097888fc903ffbec489caee79ed5 (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
#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/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"


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

PmIpkg::~PmIpkg()
{
}

int PmIpkg::runIpkg(const QString& args)
{
  pvDebug(4,"PmIpkg::runIpkg");
  QString cmd = "/usr/bin/ipkg ";
  cmd += " -dest "+settings->getDestinationName();
  cmd += " -force-defaults ";
  
  outPut->setText( outPut->text()+"<br><br>Starting to "+ args+"<br>\n");
  QString redirect = "/tmp/ipkg.pipe";
  cmd += args+" | tee "+redirect+" 2>&1&";

  outPut->setText( outPut->text() + "running:<br>\n"+cmd+"<br>\n" );
  pvDebug( 0, "Execute >"+cmd+"<" );
  int r = system(cmd.latin1());
  QFile f( redirect );
  if (  f.open(IO_ReadOnly) ) {
    QTextStream t( &f );
    QString fp;
    while ( !t.eof() )
      {
	outPut->setText( outPut->text() + t.readLine() +"\n<br>" );
      }
  }
  f.close();
  outPut->setText( outPut->text() + "\n<br><br>Finished!");
  outPut->setContentsPos(0,outPut->contentsHeight());

  if ( r == 0 )
    {
      QString param = args.left( args.find(" ") );
      QString file = args.right( args.length() - args.find(" ") );
      if ( param == "install" && settings->createLinks() )
	makeLinks( file);
    }

  return r;
}

void PmIpkg::makeLinks(QString file)
{
  outPut->setText( outPut->text() + "<br>creating links<br>" );
  QString dest = settings->getDestinationUrl();
  system(("ipkg -d "+dest+" files "+file+"> /tmp/oipkg.pipe").latin1());
  QFile f( "/tmp/oipkg.pipe" );
  if ( ! f.open(IO_ReadOnly) )return;
  QTextStream t( &f );
  QString fp;
  while ( !t.eof() )
    {
      processLinkDir( t.readLine(), dest );
    }	
  f.close();
}

void PmIpkg::processLinkDir( QString file, QString dest )
{
  QString destFile = file.right( file.length() - dest.length() );
  QFileInfo fileInfo( file );
  if ( fileInfo.isFile() )
    {
      const char *instFile = strdup( (file).ascii() );
      const char *linkFile = strdup(  (destFile).ascii());
      outPut->setText( outPut->text() + "linking: "+file+" -> "+destFile  );
      symlink( instFile, linkFile );
    }
  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()) ) {
    	processLinkDir( fi->absFilePath(), dest );      			
	++it;
      }
    }
}

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

  QString rem="To remove:<br>\n";
  QString inst="To install:<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()+"\n";
	    }
	  if ( pack->toRemove() )
	    {
	      to_remove.append( pack->name() );
	      rem += pack->name()+"\n";
	    }
	}
    }

  outPut->setText( outPut->text()+inst+rem);
  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 ) 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( QString src, 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;
  }
}