summaryrefslogtreecommitdiffstats
path: root/kommander/editor/formfile.cpp
blob: 90b9ba188da06eeaaf4eb4d73bd34076a305e822 (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
/**********************************************************************
 This file is based on TQt Designer, Copyright (C) 2000 Trolltech AS. All rights reserved.

 This file may be distributed and/or modified under the terms of the
 GNU General Public License version 2 as published by the Free Software
 Foundation and appearing in the file LICENSE.GPL included in the
 packaging of this file.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

 See http://www.trolltech.com/gpl/ for GPL licensing information.

 Modified for Kommander:
  (C) 2002-2003 Marc Britton <consume@optusnet.com.au>
  (C) 2004      Michal Rudolf <mrudolf@kdewebdev.org>

**********************************************************************/

// Other includes
#include "formfile.h"
#include "timestamp.h"
#include "formwindow.h"
#include "command.h"
#include "mainwindow.h"
#include "resource.h"
#include "workspace.h"

// TQt includes
#include <tqfile.h>
#include <tqregexp.h>
#include <statusbar.h>
#include <tqtimer.h>

// KDE includes
#include <kdebug.h>
#include <kfiledialog.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstatusbar.h>
#include <kstdguiitem.h>

#include <sys/stat.h>

FormFile::FormFile(const TQString &fn, bool temp)
    : filename(fn), fileNameTemp(temp), fw(0)
{
  TQTimer::singleShot(0, this, TQT_SLOT(init()));
}

void FormFile::init()
{
  connect(this, TQT_SIGNAL(addedFormFile(FormFile *)), MainWindow::self->workspace(),
    TQT_SLOT(formFileAdded(FormFile*)));
  connect(this, TQT_SIGNAL(removedFormFile(FormFile *)), MainWindow::self->workspace(),
    TQT_SLOT(formFileRemoved(FormFile*)));
  emit addedFormFile(this);
}

FormFile::~FormFile()
{
  if (formWindow())
    formWindow()->setFormFile(0);
}

void FormFile::setFormWindow(FormWindow *f)
{
  if (f == fw)
    return;
  if (fw)
    fw->setFormFile(0);
  fw = f;
  if (fw)
  fw->setFormFile(this);
}

void FormFile::setFileName(const TQString &fn)
{
  if (fn == filename)
    return;
  if (fn.isEmpty()) {
    fileNameTemp = true;
    if (filename.tqfind("unnamed"))
      filename = createUnnamedFileName();
    return;
  }
  filename = fn;
}

FormWindow *FormFile::formWindow() const
{
  return fw;
}

TQString FormFile::fileName() const
{
  return filename;
}

TQString FormFile::absFileName() const
{
  return filename;
}


bool FormFile::save(bool withMsgBox)
{
  if (!formWindow())
    return true;
  if (fileNameTemp)
    return saveAs();
  if (!isModified())
    return true;
  else if (withMsgBox && !formWindow()->checkCustomWidgets())
    return false;

  Resource resource(MainWindow::self);
  resource.setWidget(formWindow());
  if (!resource.save(filename, false))
  {
    if (KMessageBox::questionYesNo(MainWindow::self, i18n("Failed to save file '%1'.\n"
        "Do you want to use another file name?").tqarg(filename), TQString(), i18n("Try Another"), i18n("Do Not Try")) == KMessageBox::Yes)
      return saveAs();
    else 
      return false;
  }
  MainWindow::self->statusBar()->message(i18n("'%1' saved.").tqarg(filename), 3000);
  ::chmod(filename.local8Bit(), S_IRWXU);
  setModified(false);
  return true;
}

bool FormFile::saveAs()
{
  TQString f = fileName();
  if(fileNameTemp)
    f = TQString(formWindow()->name()).lower() + ".kmdr";
  bool saved = false;
  while (!saved) {
    TQString fn = KFileDialog::getSaveFileName(TQString(),
      i18n("*.kmdr|Kommander Files"), MainWindow::self,
      i18n("Save Form '%1' As").tqarg(formWindow()->name()));
    if (fn.isEmpty())
      return false;
    TQFileInfo fi(fn);
    if (fi.extension() != "kmdr")
      fn += ".kmdr";
    fileNameTemp = false;
    filename = fn;

    TQFileInfo relfi(filename);
    if (relfi.exists()) {
      if (KMessageBox::warningContinueCancel(MainWindow::self,
         i18n("The file already exists. Do you wish to overwrite it?"),
         i18n("Overwrite File?"), i18n("Overwrite")) == KMessageBox::Continue)
        saved = true;
      else
        filename = f;
    }
    else
      saved = true;
  }
  setModified(true);
  return save();
}

bool FormFile::close()
{
  if (formWindow())
    return formWindow()->close();
  return true;
}

bool FormFile::closeEvent()
{
  if (!isModified())
  {
    emit removedFormFile(this);
    TQFile f(filename + ".backup");
    f.remove();
    return true;
  }

  switch (KMessageBox::warningYesNoCancel(MainWindow::self, i18n("Dialog '%1' was modified."
  "Do you want to save it?").tqarg(filename), i18n("Save File?"), KStdGuiItem::save(), KStdGuiItem::discard())) {
    case KMessageBox::Yes:
      if (!save())
        return false;
    case KMessageBox::No: //fall through
      MainWindow::self->workspace()->update();
      break;
    case KMessageBox::Cancel:
      return false;
    default:
      break;
    }

  emit removedFormFile(this);
  setModified(false);
  TQFile f(filename + ".backup");
  f.remove();
  return true;
}

void FormFile::setModified(bool m)
{
  setFormWindowModified(m);
}

bool FormFile::isModified()
{
  return isFormWindowModified();
}

bool FormFile::isFormWindowModified() const
{
  if (!formWindow() || !formWindow()->commandHistory())
    return false;
  return formWindow()->commandHistory()->isModified();
}

void FormFile::setFormWindowModified(bool m)
{
  if (m == isFormWindowModified() || !formWindow() || !formWindow()->commandHistory())
    return;
  formWindow()->commandHistory()->setModified(m);
  emit somethingChanged(this);
}

void FormFile::showFormWindow()
{
  if (formWindow())
  {
    formWindow()->setFocus();
    return;
  }
  MainWindow::self->openFormWindow(filename, true, this);
}


static int ui_counter = 0;
TQString FormFile::createUnnamedFileName()
{
  return i18n("unnamed") + TQString::number(++ui_counter) + TQString(".kmdr");
}

TQString FormFile::formName() const
{
  FormFile* that = (FormFile*) this;
  if (formWindow()) {
    that->cachedFormName = formWindow()->name();
    return cachedFormName;
  }
  if (!cachedFormName.isNull())
    return cachedFormName;
  TQFile f(filename);
  if (f.open(IO_ReadOnly))
  {
    TQTextStream ts(&f);
    TQString line;
    TQString className;
    while (!ts.eof())
    {
      line = ts.readLine();
      if (!className.isEmpty())
      {
        int end = line.tqfind("</class>");
        if (end == -1)
          className += line;
        else
        {
          className += line.left(end);
          break;
        }
        continue;
      }
      int start;
      if ((start = line.tqfind("<class>")) != -1)
      {
        int end = line.tqfind("</class>");
        if (end == -1)
          className = line.mid(start + 7);
        else
        {
          className = line.mid(start + 7, end - (start + 7));
          break;
        }
      }
    }
    that->cachedFormName =  className;
  }
  if (cachedFormName.isEmpty())
    that->cachedFormName = filename;
  return cachedFormName;
}

void FormFile::formWindowChangedSomehow()
{
  emit somethingChanged(this);
}

#include "formfile.moc"