~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to noatun/noatun/modules/winskin/waSkinOperation.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  operations with skinset.
 
3
  Copyright (C) 1999  Martin Vogt
 
4
 
 
5
  This program is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU General Public License as published by
 
7
  the Free Software Foundation.
 
8
 
 
9
  For more information look at the file COPYRIGHT in this package
 
10
 
 
11
 */
 
12
 
 
13
 
 
14
#include <config.h>
 
15
#include <qdir.h>
 
16
#include <waSkinOperation.h>
 
17
 
 
18
#include "error.xpm"
 
19
#include "waSkinMapping.h"  
 
20
 
 
21
 
 
22
WaSkinOperation::WaSkinOperation() {
 
23
}
 
24
 
 
25
 
 
26
WaSkinOperation::~WaSkinOperation() {
 
27
}
 
28
 
 
29
 
 
30
int WaSkinOperation::loadSkinSet(QString skinDir,WaSkinModell* waSkinModell) {
 
31
  int i;
 
32
 
 
33
  // now load the pixmaps
 
34
  QPixmap loadPixmap;
 
35
  QFileInfo fileInfo;
 
36
  int lError=false;
 
37
 
 
38
  if (WaSkinOperation::isZippedFile(skinDir)) {
 
39
    char* tempdir=tempnam(NULL, NULL);
 
40
    QString cmd=WaSkinOperation::unzipCommand(skinDir,tempdir);
 
41
    QString dir=WaSkinOperation::doUnzip(cmd,tempdir);
 
42
    lError=WaSkinOperation::loadSkinSet(dir,waSkinModell);
 
43
    
 
44
    QString del;
 
45
    del="rm -rf ";
 
46
    del=del+tempdir;
 
47
    WaSkinOperation::doSystemCall(del);
 
48
 
 
49
    free(tempdir);
 
50
    waSkinModell->setSkinDirectory(skinDir);
 
51
    if (lError) {
 
52
    }
 
53
    return false;
 
54
  }
 
55
 
 
56
  for(i=0;i<_WA_SKIN_ENTRIES;i++) {
 
57
    WaSkinDesc* waSkinDesc=waSkinModell->getWaSkinDesc(i);
 
58
    QPixmap* pixmap=waSkinDesc->getPixmap();
 
59
    QString file=waSkinDesc->getFilename();
 
60
 
 
61
    int lFound=WaSkinOperation::getPixmap(skinDir,file,&loadPixmap);
 
62
 
 
63
    int x=waSkinDesc->getX();
 
64
    int y=waSkinDesc->getY();
 
65
    int width=waSkinDesc->getWidth();
 
66
    int height=waSkinDesc->getHeight();
 
67
    pixmap->resize(width,height);
 
68
    if (lFound == true) {
 
69
      bitBlt(pixmap,0,0,&loadPixmap,x,y,width,height);
 
70
    } else {
 
71
      lError=true;
 
72
    }
 
73
    waSkinModell->emitChangeEvent(i);
 
74
  }
 
75
  waSkinModell->setSkinDirectory(skinDir);
 
76
  if (lError) {
 
77
  }
 
78
  return false;
 
79
}
 
80
 
 
81
 
 
82
void WaSkinOperation::makeEmptySkinModell(WaSkinModell* waSkinModell) {
 
83
  int i;
 
84
  char id[10];
 
85
  id[9]=0;
 
86
 
 
87
  // now make a lokal copy of global struct
 
88
  QPixmap pixmap(error_xpm);
 
89
  for(i=0;i<_WA_SKIN_ENTRIES;i++) {
 
90
    WaSkinDesc* waSkinDesc=waSkinModell->
 
91
      getWaSkinDesc(globalMapFromFile[i].posId);
 
92
 
 
93
    waSkinDesc->setFilename(globalMapFromFile[i].filename);
 
94
    waSkinDesc->setPosId(globalMapFromFile[i].posId);
 
95
    waSkinDesc->setX(globalMapFromFile[i].x);
 
96
    waSkinDesc->setY(globalMapFromFile[i].y);
 
97
    waSkinDesc->setWidth(globalMapFromFile[i].width);
 
98
    waSkinDesc->setHeight(globalMapFromFile[i].height);
 
99
    waSkinDesc->setPixmap(&pixmap);
 
100
 
 
101
    QPixmap* tmpPixmap=waSkinDesc->getPixmap();
 
102
    QPainter paint;
 
103
    paint.begin( tmpPixmap );
 
104
    snprintf((char*)&id,9,"%d",waSkinDesc->getPosId());
 
105
    paint.drawText ( 0, 10, (char*)&id);
 
106
    paint.end();  
 
107
 
 
108
 
 
109
    //
 
110
    // Now find the mapToGlobal id an add the information
 
111
    //
 
112
    int id=waSkinDesc->getPosId();
 
113
    int lFound=false;
 
114
    for(i=0;i<_WA_SKIN_ENTRIES;i++) {
 
115
      if (bmpToGui[i][0]==id) {
 
116
        int globalPos=bmpToGui[i][1];
 
117
        waSkinDesc->setWidgetId(globalPos);
 
118
        if (globalPos != -1) {
 
119
          waSkinDesc->setWidgetX(globalMapToGui[globalPos].x);
 
120
          waSkinDesc->setWidgetY(globalMapToGui[globalPos].y);
 
121
          waSkinDesc->setWidgetWidth(globalMapToGui[globalPos].width);
 
122
          waSkinDesc->setWidgetHeight(globalMapToGui[globalPos].height);
 
123
          waSkinModell->emitChangeEvent(i);
 
124
        }
 
125
        lFound=true;
 
126
        break;
 
127
        
 
128
      }
 
129
    }
 
130
    if (lFound == false) {
 
131
      exit(0);
 
132
    }
 
133
 
 
134
  }
 
135
  // now load the mapping in the modell
 
136
  for(i=0;i<_WA_MAPPING_ENTRIES;i++) {
 
137
    WaSkinDesc* waSkinDesc=waSkinModell
 
138
      ->getWaSkinMapDesc(globalMapToGui[i].posId);
 
139
    waSkinDesc->setWidgetId(globalMapToGui[i].posId);
 
140
    waSkinDesc->setWidgetX(globalMapToGui[i].x);
 
141
    waSkinDesc->setWidgetY(globalMapToGui[i].y);
 
142
    waSkinDesc->setWidgetWidth(globalMapToGui[i].width);
 
143
    waSkinDesc->setWidgetHeight(globalMapToGui[i].height);
 
144
  }
 
145
 
 
146
}
 
147
 
 
148
 
 
149
int WaSkinOperation::getPixmap(QString dir,QString fname,QPixmap* target) {
 
150
  int back;
 
151
  QDir qdir(dir);
 
152
 
 
153
  if (WaSkinOperation::isZippedFile(dir)) {
 
154
    char* tempdir=tempnam(NULL, NULL);
 
155
    QString cmd=WaSkinOperation::unzipCommand(dir,tempdir);
 
156
    QString skinDir=WaSkinOperation::doUnzip(cmd,tempdir);
 
157
    back=WaSkinOperation::getPixmap(skinDir,fname,target);
 
158
 
 
159
    QString del;
 
160
    del="rm -rf ";
 
161
    del=del+tempdir;
 
162
    WaSkinOperation::doSystemCall(del);
 
163
    free(tempdir);
 
164
    return back;
 
165
  }
 
166
 
 
167
 
 
168
  if (qdir.exists() == false) {
 
169
    return false;
 
170
  }
 
171
 
 
172
  QString basename=WaSkinOperation::baseName(fname);
 
173
 
 
174
  back=WaSkinOperation::getPixmapExt(&qdir,basename,"bmp",target);
 
175
  if (back == true) {
 
176
    return true;
 
177
  }
 
178
  back=WaSkinOperation::getPixmapExt(&qdir,basename,"xpm",target);
 
179
  if (back == true) {
 
180
    return true;
 
181
  }
 
182
  return false;
 
183
}
 
184
  
 
185
 
 
186
int WaSkinOperation::getPixmapExt(QDir* dir,QString basename,
 
187
                                  QString extension,QPixmap* target) {
 
188
 
 
189
 
 
190
  QFileInfo fileInfo;
 
191
  QStringList strList=dir->entryList();
 
192
 
 
193
  QString cmpString2=basename.upper();
 
194
  
 
195
  int n=strList.count();
 
196
  int i;
 
197
  // this strList thing is a hack for qt-1.40
 
198
  strList.first();
 
199
  for(i=0;i<n;i++) {
 
200
    QString file=strList[i];
 
201
 
 
202
    QFileInfo fileInfo;
 
203
    fileInfo.setFile(file);
 
204
    if (fileInfo.isDir()) {
 
205
      continue;
 
206
    }
 
207
    QString cmpString1=fileInfo.baseName().upper();
 
208
    
 
209
    if (cmpString1==cmpString2) {
 
210
      QString file=dir->absPath()+"/"+QString(fileInfo.filePath());
 
211
      target->load(file);
 
212
      return true;
 
213
    }
 
214
  }
 
215
 
 
216
  // now the filename mapping 1.8x -> 2.0
 
217
  if (basename.upper() == "VOLUME") {
 
218
    return WaSkinOperation::getPixmapExt(dir,QString("VOLBAR"),
 
219
                                         extension,target);
 
220
  }
 
221
  if (basename.upper() == "NUMBERS") {
 
222
    return WaSkinOperation::getPixmapExt(dir,QString("NUMS_EX"),
 
223
                                         extension,target);
 
224
  } 
 
225
 
 
226
  return false;
 
227
}
 
228
 
 
229
 
 
230
int WaSkinOperation::isZippedFile(QString skinDir) {
 
231
  QFileInfo fileInfo;
 
232
  QString ending;
 
233
  fileInfo.setFile(skinDir);
 
234
  ending=fileInfo.extension().upper();
 
235
  if (ending == "") {
 
236
    return false;
 
237
  }
 
238
  if ( (ending == "ZIP") ||
 
239
       (ending == "TAR") ||
 
240
       (ending == "TAR.GZ") ||
 
241
       (ending == "TGZ") ||
 
242
       (ending == "GZ" ) || 
 
243
       (ending == "TAR.BZ2") ||
 
244
       (ending == "BZ2") ) {
 
245
    return true;
 
246
  }
 
247
  return false;
 
248
}
 
249
 
 
250
 
 
251
QString WaSkinOperation::unzipCommand(QString skinDir,QString tempdir) {
 
252
  QString tmp;
 
253
  QString back;
 
254
  QString ending;
 
255
  QFileInfo fileInfo;
 
256
  
 
257
  fileInfo.setFile(skinDir);
 
258
  ending=fileInfo.extension().upper();
 
259
  if (ending == "ZIP") {
 
260
    back=getenv("UNZIPCMD");
 
261
    if (back == NULL) {
 
262
      back="unzip ";
 
263
    }
 
264
    tmp=back+" -o -j \""+skinDir+"\" -d "+tempdir;
 
265
 
 
266
    return tmp;
 
267
  }
 
268
 
 
269
  if ( (ending == "GZ") ) {
 
270
    back="gzip -d ";
 
271
    tmp=back+skinDir;
 
272
    return tmp;
 
273
  }
 
274
 
 
275
  if ( (ending == "TAR.GZ") || 
 
276
       (ending == "TGZ") ) {
 
277
    back=getenv("TARCMD");
 
278
    if (back == NULL) {
 
279
      back="gzip -cd ";
 
280
    }
 
281
    tmp=back+"\""+skinDir+"\" >"+tempdir+"/tmp.tar";
 
282
    tmp=tmp+";"+unzipCommand(QString(tempdir)+"/tmp.tar",tempdir);
 
283
    return tmp;
 
284
  }
 
285
 
 
286
  if (ending == "TAR") {
 
287
    back=getenv("TARCMD");
 
288
    if (back == NULL) {
 
289
      back="tar xvf ";
 
290
    }
 
291
    tmp=back+"\""+skinDir+"\" -C "+tempdir;
 
292
    return tmp;
 
293
  }
 
294
 
 
295
 
 
296
  if (ending == "BZ2") {
 
297
    if (back == NULL) {
 
298
      back="bunzip2 ";
 
299
    }
 
300
    tmp=back+" \""+skinDir+"\"";
 
301
    return tmp;
 
302
  }
 
303
 
 
304
  if (ending == "TAR.BZ2") {
 
305
    if (back == NULL) {
 
306
      back="bunzip2 ";
 
307
    }
 
308
    tmp=back+"-cdf \""+skinDir+"\" >"+tempdir+"/tmp.tar";
 
309
    tmp=tmp+";"+unzipCommand(QString(tempdir)+"/tmp.tar",tempdir);
 
310
    return tmp;
 
311
  }
 
312
 
 
313
  return NULL;
 
314
}
 
315
 
 
316
 
 
317
QString WaSkinOperation::doUnzip(QString unzipCommand,QString tempdir) {
 
318
  mkdir(tempdir.latin1(), 0x1ed);  /* Permissions: 755 */
 
319
  
 
320
  
 
321
  WaSkinOperation::doSystemCall(unzipCommand);
 
322
  return findMainBMP(tempdir);
 
323
}
 
324
 
 
325
 
 
326
QString WaSkinOperation::findMainBMP(QString tempdir) {
 
327
  QDir dir;
 
328
  dir.setPath(tempdir);
 
329
  // this strList is a big hack to compile with qt-1.40
 
330
  QStringList strList=dir.entryList();
 
331
  int n=strList.count();
 
332
  int i;
 
333
  QString path(dir.absPath());
 
334
  strList.first();
 
335
  for(i=0;i<n;i++) {
 
336
    QString file=strList[i];
 
337
 
 
338
    if ((file != ".") && (file != "..")) {
 
339
      QFileInfo fileInfo;
 
340
      QString testDir=path+"/"+file;
 
341
      fileInfo.setFile(testDir);
 
342
      if (fileInfo.isDir()) {
 
343
        QString back=findMainBMP(testDir);
 
344
        if (back != "") {
 
345
          return back;
 
346
        }
 
347
        continue;
 
348
      }
 
349
      QString cmpString=fileInfo.baseName().upper();
 
350
      if (cmpString=="MAIN") {
 
351
        return QString(tempdir)+"/";
 
352
      }
 
353
    }
 
354
    
 
355
  }
 
356
  return "";
 
357
}
 
358
 
 
359
 
 
360
 
 
361
QString WaSkinOperation::baseName(QString filename) {
 
362
  int pos = filename.find('.');
 
363
  if ( pos == -1 ) {
 
364
    return filename;
 
365
  } else {
 
366
    return filename.left(pos);
 
367
  }
 
368
 
 
369
}
 
370
 
 
371
 
 
372
 
 
373
void WaSkinOperation::doSystemCall(QString call) {
 
374
  system(call.latin1());
 
375
  if (errno == 127) {
 
376
  }
 
377
  if (errno < 0) {
 
378
    perror("error is:");
 
379
  } 
 
380
}
 
381