summaryrefslogtreecommitdiffstats
path: root/kimagemapeditor/qextfileinfo.cpp
blob: 440e9c78f3d042c1e8ed6bd877c015a088e6cf37 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
    From WebMaker - KDE HTML Editor
    Copyright (C) 1998, 1999 Alexei Dets <dets@services.ru>

    Rewritten for Quanta Plus: (C) 2002 Andras Mantia <amantia@freemail.hu>

    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.
*/


//qt includes
#include <tqdir.h>
#include <tqapplication.h>
#include <tqptrlist.h>
#include <tqstringlist.h>
#include <tqregexp.h>
#include <tqtimer.h>

//kde includes
#include <kurl.h>
#include <kio/job.h>
#include <kio/netaccess.h>
#include <kio/scheduler.h>
#include <kdirlister.h>
#include <kfileitem.h>
#include <kglobal.h>
#include <kdebug.h>

//app includes
#include "qextfileinfo.h"

TQString QExtFileInfo::lastErrorMsg = "";

/** create a relative short url based in baseURL*/
KURL QExtFileInfo::toRelative(const KURL& urlToConvert,const KURL& baseURL)
{
  KURL resultURL = urlToConvert;
  if (urlToConvert.protocol() == baseURL.protocol())
  {
    TQString path = urlToConvert.path();
    TQString basePath = baseURL.path(1);
    if (path.startsWith("/"))
    {
      path.remove(0, 1);
      basePath.remove(0, 1);
      if ( basePath.right(1) != "/" ) basePath.append("/");

      int pos=0;
      int pos1=0;
      for (;;)
      {
        pos=path.find("/");
        pos1=basePath.find("/");
        if ( pos<0 || pos1<0 ) break;
        if ( path.left(pos+1 ) == basePath.left(pos1+1) )
        {
          path.remove(0, pos+1);
          basePath.remove(0, pos1+1);
        }
        else
          break;
      };

      if ( basePath == "/" ) basePath="";
      int level = basePath.contains("/");
      for (int i=0; i<level; i++)
      {
        path="../"+path;
      };
    }

    resultURL.setPath(TQDir::cleanDirPath(path));
  }

  if (urlToConvert.path().endsWith("/")) resultURL.adjustPath(1);
  return resultURL;
}
/** convert relative filename to absolute */
KURL QExtFileInfo::toAbsolute(const KURL& urlToConvert,const KURL& baseURL)
{
  KURL resultURL = urlToConvert;
  if (urlToConvert.protocol() == baseURL.protocol() && !urlToConvert.path().startsWith("/"))
  {
    int pos;
    TQString cutname = urlToConvert.path();
    TQString cutdir = baseURL.path(1);
    while ( (pos = cutname.find("../")) >=0 )
    {
       cutname.remove( 0, pos+3 );
       cutdir.remove( cutdir.length()-1, 1 );
       cutdir.remove( cutdir.findRev('/')+1 , 1000);
    }
    resultURL.setPath(TQDir::cleanDirPath(cutdir+cutname));
  }

  if (urlToConvert.path().endsWith("/")) resultURL.adjustPath(1);
  return resultURL;
}

/** All files in a dir.
  The return will also contain the name of the subdirectories.
  This is needed for empty directory adding/handling. (Andras)
  Currently works only for local directories
*/
KURL::List QExtFileInfo::allFiles( const KURL& path, const TQString& mask)
{
  QExtFileInfo internalFileInfo;
  return internalFileInfo.allFilesInternal(path, mask);
}

KURL::List QExtFileInfo::allFilesRelative( const KURL& path, const TQString& mask)
{
  QExtFileInfo internalFileInfo;
  KURL::List r = internalFileInfo.allFilesInternal( path, mask);

  KURL::List::Iterator it;
  for ( it = r.begin(); it != r.end(); ++it )
  {
    *it = QExtFileInfo::toRelative( *it, path );
  }

  return r;
}

bool QExtFileInfo::createDir( const KURL& path )
{
  int i=0;
  bool result;
  KURL dir1, dir2 = KURL();
  while ( !exists(path) && dir2.path() != path.path() )
  {
    dir1 = path;
    dir2 = path;

    dir1=cdUp(dir1);
    while ( !exists(dir1) && dir1.path() != "/" )
    {
      dir1=cdUp(dir1);
      dir2=cdUp(dir2);
    //  debug(d1);
    }
  //  dir2.setPath(dir2.path(-1));
    result = TDEIO::NetAccess::mkdir(dir2, 0L, -1);
    i++;
  }
 result = exists(path);
 return result;
}

KURL QExtFileInfo::cdUp(const KURL &url)
{
  KURL u = url;
  TQString dir = u.path(-1);
  while ( !dir.isEmpty() && dir.right(1) != "/" )
  {
    dir.remove( dir.length()-1,1);
  }
  u.setPath(dir);
  return u;
}

TQString QExtFileInfo::shortName(const TQString &fname)
{
  return fname.section("/",-1);
}

KURL QExtFileInfo::path( const KURL &url )
{
  return KURL( url.directory(false,false) );
}

KURL QExtFileInfo::home()
{
  KURL url;
  url.setPath(TQDir::currentDirPath()+"/");
  return url;
}


bool QExtFileInfo::exists(const KURL& a_url)
{
// Andras: Don't use it now, as it brings up an extra dialog and need manual
// intervention when usign fish
// return TDEIO::NetAccess::exists(a_url, false);

// No dialog when stating.
 if ( a_url.isLocalFile() )
 {
    return TQFile::exists( a_url.path() );
 } else
 {
  QExtFileInfo internalFileInfo;
  return internalFileInfo.internalExists(a_url);
 }
}

/* Synchronouse copy, like NetAccess::file_copy in KDE 3.2 */
bool QExtFileInfo::copy( const KURL& src, const KURL& target, int permissions,
 bool overwrite, bool resume, TQWidget* window )
{
  QExtFileInfo internalFileInfo;
  return internalFileInfo.internalCopy( src, target, permissions, overwrite, resume, window );
}

/** No descriptions */
KURL::List QExtFileInfo::allFilesInternal(const KURL& startURL, const TQString& mask)
{
  dirListItems.clear();
  if (internalExists(startURL))
  {
    lstFilters.setAutoDelete(true);
    lstFilters.clear();
    // Split on white space
    TQStringList list = TQStringList::split( ' ', mask );
    for ( TQStringList::Iterator it = list.begin(); it != list.end(); ++it )
       lstFilters.append( new TQRegExp(*it, false, true ) );

    bJobOK = true;
    TDEIO::ListJob *job = TDEIO::listRecursive(startURL, false, true);
    connect(job, TQT_SIGNAL(entries(TDEIO::Job *, const TDEIO::UDSEntryList&)),
            this, TQT_SLOT(slotNewEntries(TDEIO::Job *, const TDEIO::UDSEntryList&)));
    connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
             this, TQT_SLOT( slotResult (TDEIO::Job *) ) );

 //   kdDebug(24000) << "Now listing: " << startURL.url() << endl;
    enter_loop();
    lstFilters.clear();
    if (!bJobOK)
    {
 //     kdDebug(24000) << "Error while listing "<< startURL.url() << endl;
      dirListItems.clear();
    }
  }
  return dirListItems;
}


//Some hackery from TDEIO::NetAccess as they do not do exactly what we want
/* return true if the url exists*/
bool QExtFileInfo::internalExists(const KURL& url)
{
  bJobOK = true;
 // kdDebug(24000)<<"QExtFileInfo::internalExists"<<endl;
  TDEIO::StatJob * job = TDEIO::stat( url, false);
  job->setDetails(0);
  job->setSide(false); //check the url for writing
  connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
           this, TQT_SLOT( slotResult (TDEIO::Job *) ) );

  //To avoid lock-ups, start a timer.
  TQTimer::singleShot(10*1000, this,TQT_SLOT(slotTimeout()));
//  kdDebug(24000)<<"QExtFileInfo::internalExists:before enter_loop"<<endl;
  enter_loop();
//  kdDebug(24000)<<"QExtFileInfo::internalExists:after enter_loop"<<endl;
  return bJobOK;
}

bool QExtFileInfo::internalCopy(const KURL& src, const KURL& target, int permissions,
                                bool overwrite, bool resume, TQWidget* window)
{
  bJobOK = true; // success unless further error occurs

  TDEIO::Scheduler::checkSlaveOnHold(true);
  TDEIO::Job * job = TDEIO::file_copy( src, target, permissions, overwrite, resume );
  job->setWindow (window);
  connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
           this, TQT_SLOT( slotResult (TDEIO::Job *) ) );

  enter_loop();
  return bJobOK;
}


void tqt_enter_modal( TQWidget *widget );
void tqt_leave_modal( TQWidget *widget );

void QExtFileInfo::enter_loop()
{
  TQWidget dummy(0,0,WType_Dialog | WShowModal);
  dummy.setFocusPolicy( TQ_NoFocus );
  tqt_enter_modal(&dummy);
//  kdDebug(24000)<<"QExtFileInfo::enter_loop:before tqApp->enter_loop()"<<endl;
  tqApp->enter_loop();
//  kdDebug(24000)<<"QExtFileInfo::enter_loop:after tqApp->enter_loop()"<<endl;
  tqt_leave_modal(&dummy);
}

void QExtFileInfo::slotResult( TDEIO::Job * job )
{
  bJobOK = !job->error();
  if ( !bJobOK )
  {
    if ( !lastErrorMsg )
     lastErrorMsg = job->errorString();
  }
  if ( job->isA("TDEIO::StatJob") )
    m_entry = static_cast<TDEIO::StatJob *>(job)->statResult();
  tqApp->exit_loop();
}

void QExtFileInfo::slotNewEntries(TDEIO::Job *job, const TDEIO::UDSEntryList& udsList)
{
  KURL url = static_cast<TDEIO::ListJob *>(job)->url();
  url.adjustPath(-1);
  // avoid creating these TQStrings again and again
  static const TQString& dot = TDEGlobal::staticQString(".");
  static const TQString& dotdot = TDEGlobal::staticQString("..");

  TDEIO::UDSEntryListConstIterator it = udsList.begin();
  TDEIO::UDSEntryListConstIterator end = udsList.end();
  KURL itemURL;
  for ( ; it != end; ++it )
  {
    TQString name;

    // find out about the name
    TDEIO::UDSEntry::ConstIterator entit = (*it).begin();
    for( ; entit != (*it).end(); ++entit )
      if ( (*entit).m_uds == TDEIO::UDS_NAME )
      {
        name = (*entit).m_str;
        break;
      }

    if ( ! name.isEmpty() && name != dot && name != dotdot)
    {
      KFileItem* item = new KFileItem( *it, url, false, true );
      itemURL = item->url();
      if (item->isDir()) itemURL.adjustPath(1);
      for ( TQPtrListIterator<TQRegExp> filterIt( lstFilters ); filterIt.current(); ++filterIt )
      if ( filterIt.current()->exactMatch( item->text() ) )
           dirListItems.append(itemURL);
      delete item;
    }
  }
}

/** Timeout occured while waiting for some network function to return. */
void QExtFileInfo::slotTimeout()
{
  bJobOK = false;
  tqApp->exit_loop();
}
#include "qextfileinfo.moc"