summaryrefslogtreecommitdiffstats
path: root/quanta/utility/quantanetaccess.cpp
blob: 375f6f6d50e91069ab4a3dcea108ca3b4031d912 (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
/***************************************************************************
                          quantanetaccess.h  -  description
                             -------------------
    begin                : Jun 21 2004
    copyright            : (C) 2004 by Jens Herden <jhe@tdewebdev.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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; version 2 of the License.               *
 *                                                                         *
 ***************************************************************************/

#include <tqstring.h>
#include <tqwidget.h>
#include <tqdir.h>

#include <kio/netaccess.h>
#include <kurl.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kfileitem.h>
#include <kstringhandler.h>

#include "qextfileinfo.h"
#include "quantanetaccess.h"
#include "project.h"

bool QuantaNetAccess::upload(const TQString& src, const KURL& target, TQWidget* window, bool confirm)
{
  bool ok = KIO::NetAccess::upload(src, target, window);
  if (ok) {
    checkProjectInsert(target, window, confirm);
  } else {
    if (confirm)
      errorMsg(window);
  }
  return ok;
}


bool QuantaNetAccess::file_copy( const KURL& src, const KURL& target, int permissions,
                                 bool overwrite, bool resume, TQWidget* window, bool confirm)
{
  bool ok = KIO::NetAccess::file_copy( src, target, permissions, overwrite, resume, window );
  if (ok) {
    checkProjectInsert(target, window, confirm);
  } else {
    if (confirm)
      errorMsg(window);
  }
  return ok;
}


bool QuantaNetAccess::file_move( const KURL& src, const KURL& target, int permissions,
                                 bool overwrite, bool resume, TQWidget* window, bool confirm)
{
  // don't ask if move is inside of the project
  bool oldConfirm = confirm;
  if ( Project::ref()->projectBaseURL().isParentOf(src) &&
       Project::ref()->projectBaseURL().isParentOf(target) )
  {
    confirm = false;
  }
  if ( !checkProjectRemove(src, window, confirm)) {
    return false;
  }
  bool ok = KIO::NetAccess::file_move( src, target, permissions, overwrite, resume, window );
  if (ok) {
    checkProjectInsert(target, window, confirm);
  } else {
    if (oldConfirm)
      errorMsg(window);
  }
  return ok;
}


bool QuantaNetAccess::dircopy( const KURL::List & srcList, const KURL & target, TQWidget* window, bool confirm )
{
  bool ok = KIO::NetAccess::dircopy( srcList, target, window );
  if (ok) {
    KURL url;
    for ( KURL::List::ConstIterator it = srcList.begin(); it != srcList.end(); ++it ) {
      url = target;
      url.adjustPath(+1);
      url.setFileName((*it).fileName());
      checkProjectInsert(url, window, confirm);
    }
  } else {
    if (confirm)
      errorMsg(window);
  }
  return ok;
}


bool QuantaNetAccess::move( const KURL::List& srcList, const KURL& target, TQWidget* window, bool confirm )
{
  KURL targetURL = adjustURL(target);
  bool oldConfirm = confirm;
  bool moveInsideProject = false;
  bool targetInProject = Project::ref()->projectBaseURL().isParentOf(targetURL);
  KURL url;
  // first we ask about the URLs in the list without actually removing them from the project
  for ( KURL::List::ConstIterator it = srcList.begin(); it != srcList.end(); ++it ) {
    //don't ask if move is inside of the project
    url = adjustURL(*it);
    if (targetInProject && Project::ref()->projectBaseURL().isParentOf(url) )
    {
      confirm = false;
      moveInsideProject = true;
    }
    if ( !checkProjectRemove(*it, window, confirm, false)) {
      return false;
    confirm = oldConfirm;
    }
  }
  // all URLs are confirmed, we remove them from the project
  for ( KURL::List::ConstIterator it = srcList.begin(); it != srcList.end(); ++it ) {
    if ( Project::ref()->projectBaseURL().isParentOf(*it) )
      Project::ref()->slotRemove(*it);
  }
  bool ok = KIO::NetAccess::move( srcList, targetURL, window );
  if (ok) {
    KURL url;
    for ( KURL::List::ConstIterator it = srcList.begin(); it != srcList.end(); ++it ) {
      url = target;
      url.adjustPath(+1);
      url.setFileName((*it).fileName());
      checkProjectInsert(url, window, confirm);
      Project::ref()->urlMoved(*it, url);
   }
  } else {
    if (confirm)
      errorMsg(window);
  }
  return ok;
}


bool QuantaNetAccess::del( const KURL & url, TQWidget* window, bool confirm )
{
  if ( !checkProjectDel(url, window, confirm)) {
    return false;
  }
  bool ok = KIO::NetAccess::del( url, window );
  if (!ok && confirm)
  {
      errorMsg(window);
  }
  return ok;
}


bool QuantaNetAccess::mkdir( const KURL & url, TQWidget* window, int permissions, bool confirm )
{
  KURL u = url;
  u.adjustPath(-1); //some servers refuse to create directories ending with a slash
  bool ok = KIO::NetAccess::mkdir( u, window, permissions );
  if (ok) {
    checkProjectInsert(url, window, confirm);
  } else {
    if (confirm)
      errorMsg(window);
  }
  return ok;
}


void QuantaNetAccess::checkProjectInsert(const KURL& target, TQWidget* window, bool confirm)
{
  if ( !Project::ref()->hasProject()) return;
  KURL saveUrl = adjustURL(target);
  KURL baseURL = Project::ref()->projectBaseURL();
  if ( baseURL.isParentOf(saveUrl) && !Project::ref()->contains(saveUrl) )
  {
    if (confirm)
    {
      TQString nice = QExtFileInfo::toRelative(saveUrl, baseURL).path();
      nice = KStringHandler::lsqueeze(nice, 60);
      if ( KMessageBox::Yes != KMessageBox::questionYesNo(window, i18n("<qt>Do you want to add <br><b>%1</b><br> to the project?</qt>").arg(nice), i18n("Add to Project"), KStdGuiItem::add(), i18n("Do Not Add"), "AddToProject") )
      {
        return;
      }
    }
    KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, saveUrl);
    if ( fileItem.isDir() )
      Project::ref()->slotAddDirectory(saveUrl, false);
    else
      Project::ref()->slotInsertFile(saveUrl);
  }
}


bool QuantaNetAccess::checkProjectRemove(const KURL& src, TQWidget* window, bool confirm, bool remove)
{
  if ( !Project::ref()->hasProject() ) return true;
  KURL url = adjustURL(src);
  KURL baseURL = Project::ref()->projectBaseURL();
  if ( baseURL.isParentOf(url) && Project::ref()->contains(url) )
  {
    if (confirm)
    {
      TQString nice = QExtFileInfo::toRelative(url, baseURL).path();
      nice = KStringHandler::lsqueeze(nice, 60);
      if ( KMessageBox::Continue != KMessageBox::warningContinueCancel(window, i18n("<qt>Do you really want to remove <br><b>%1</b><br> from the project?</qt>").arg(nice), i18n("Remove From Project"), KStdGuiItem::remove(), "RemoveFromProject") )
      {
        return false;
      }
    }
    if (remove)
      Project::ref()->slotRemove(url);
  }
  return true;
}


bool QuantaNetAccess::checkProjectDel(const KURL& src, TQWidget* window, bool confirm)
{
  KURL url = adjustURL(src);
  if ( Project::ref()->hasProject() )
  {
    if ( Project::ref()->projectBaseURL().isParentOf(url) && Project::ref()->contains(url) )
    {
      if (confirm)
      {
        TQString nice = url.prettyURL(0, KURL::StripFileProtocol);
        nice = KStringHandler::csqueeze(nice, 60);
        if ( KMessageBox::Continue != KMessageBox::warningContinueCancel(window, i18n("<qt>Do you really want to delete <br><b>%1</b><br> and remove it from the project?</qt>").arg(nice), i18n("Delete & Remove From Project"), KStdGuiItem::del(), "DeleteAndRemoveFromProject") )
        {
          return false;
        }
      }
      Project::ref()->slotRemove(url);
      return true;
    }
  }
  // confirm normal delete if wanted
  if (confirm) {
    TQString nice = url.prettyURL(0, KURL::StripFileProtocol);
    nice = KStringHandler::csqueeze(nice, 60);
    return (KMessageBox::Continue == KMessageBox::warningContinueCancel(window, i18n("<qt>Do you really want to delete <br><b>%1</b>?</qt>").arg(nice), i18n("Delete File or Folder"), KStdGuiItem::del(), "DeleteFileOrFolder") );
  }
  return true;
}

KURL QuantaNetAccess::adjustURL(const KURL &url)
{
   KURL u = url;
   if ( u.isLocalFile() )
   {
      TQDir dir(u.path());
      u.setPath(dir.canonicalPath());
    }
    return u;
}


void QuantaNetAccess::errorMsg(TQWidget* window)
{
  TQString msg = KIO::NetAccess::lastErrorString();
  if ( !msg.isEmpty())
    KMessageBox::sorry(window, msg);
}