• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

  • kate
  • app
katesavemodifieddialog.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2004 Joseph Wenninger <jowenn@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "katesavemodifieddialog.h"
20 #include "katesavemodifieddialog.moc"
21 
22 #include <tdelocale.h>
23 #include <tqlistview.h>
24 #include <tdelistview.h>
25 #include <kguiitem.h>
26 #include <kactivelabel.h>
27 #include <kstdguiitem.h>
28 #include <tqvbox.h>
29 #include <tqlabel.h>
30 #include <tqpushbutton.h>
31 #include <kiconloader.h>
32 #include <tdemessagebox.h>
33 #include <kdebug.h>
34 #include <kencodingfiledialog.h>
35 #include <tdetexteditor/encodinginterface.h>
36 
37 class AbstractKateSaveModifiedDialogCheckListItem:public TQCheckListItem {
38 public:
39  AbstractKateSaveModifiedDialogCheckListItem(TQListViewItem *parent,const TQString& title, const TQString& url):TQCheckListItem(parent,title,TQCheckListItem::CheckBox) {
40  setText(1,url);
41  setOn(true);
42  setState(InitialState);
43  }
44  virtual ~AbstractKateSaveModifiedDialogCheckListItem() {
45  }
46  virtual bool synchronousSave(TQWidget *dialogParent)=0;
47  enum STATE{InitialState,SaveOKState,SaveFailedState};
48  STATE state() const { return m_state;}
49  void setState(enum STATE state) {
50  m_state=state;
51  TDEIconLoader *loader = TDEGlobal::instance()->iconLoader();
52  switch (state) {
53  case InitialState:
54  setPixmap(0,TQPixmap());
55  break;
56  case SaveOKState:
57  setPixmap(0,loader->loadIcon("ok",TDEIcon::NoGroup,height()));
58  break;
59  case SaveFailedState:
60  setPixmap(0,loader->loadIcon("cancel",TDEIcon::NoGroup,height()));
61  break;
62  }
63  }
64 private:
65  STATE m_state;
66 };
67 
68 class KateSaveModifiedDocumentCheckListItem:public AbstractKateSaveModifiedDialogCheckListItem {
69 public:
70  KateSaveModifiedDocumentCheckListItem(TQListViewItem *parent,Kate::Document *document):AbstractKateSaveModifiedDialogCheckListItem(parent,document->docName(),document->url().prettyURL()){
71  m_document=document;
72  }
73  virtual ~KateSaveModifiedDocumentCheckListItem() {
74  }
75  virtual bool synchronousSave(TQWidget *dialogParent) {
76  if (m_document->url().isEmpty() ) {
77  KEncodingFileDialog::Result r=KEncodingFileDialog::getSaveURLAndEncoding(
78  KTextEditor::encodingInterface(m_document)->encoding(),TQString::null,TQString::null,dialogParent,i18n("Save As (%1)").arg(m_document->docName()));
79 
80  m_document->setEncoding( r.encoding );
81  if (!r.URLs.isEmpty()) {
82  KURL tmp = r.URLs.first();
83  if ( !m_document->saveAs( tmp ) ) {
84  setState(SaveFailedState);
85  setText(1,m_document->url().prettyURL());
86  return false;
87  } else {
88  bool sc=m_document->waitSaveComplete();
89  setText(1,m_document->url().prettyURL());
90  if (!sc) {
91  setState(SaveFailedState);
92  return false;
93  } else {
94  setState(SaveOKState);
95  return true;
96  }
97  }
98  } else {
99  setState(SaveFailedState);
100  return false;
101  }
102  } else { //document has an exising location
103  if ( !m_document->save() ) {
104  setState(SaveFailedState);
105  setText(1,m_document->url().prettyURL());
106  return false;
107  } else {
108  bool sc=m_document->waitSaveComplete();
109  setText(1,m_document->url().prettyURL());
110  if (!sc) {
111  setState(SaveFailedState);
112  return false;
113  } else {
114  setState(SaveOKState);
115  return true;
116  }
117  }
118 
119  }
120 
121  return false;
122 
123  }
124 private:
125  Kate::Document *m_document;
126 };
127 
128 KateSaveModifiedDialog::KateSaveModifiedDialog(TQWidget *parent, TQPtrList<Kate::Document> documents):
129  KDialogBase( parent, "KateSaveModifiedDialog", true, i18n("Save Documents"), Yes | No | Cancel) {
130 
131  KGuiItem saveItem=KStdGuiItem::save();
132  saveItem.setText(i18n("&Save Selected"));
133  setButtonGuiItem(KDialogBase::Yes,saveItem);
134 
135  setButtonGuiItem(KDialogBase::No,KStdGuiItem::dontSave());
136 
137  KGuiItem cancelItem=KStdGuiItem::cancel();
138  cancelItem.setText(i18n("&Abort Closing"));
139  setButtonGuiItem(KDialogBase::Cancel,cancelItem);
140 
141  TQVBox *box=makeVBoxMainWidget();
142  new KActiveLabel(i18n("<qt>The following documents have been modified. Do you want to save them before closing?</qt>"),box);
143  m_list=new TDEListView(box);
144  m_list->addColumn(i18n("Title"));
145  m_list->addColumn(i18n("Location"));
146  m_list->setRootIsDecorated(true);
147  m_list->setResizeMode(TQListView::LastColumn);
148  if (0) {
149  m_projectRoot=new TQListViewItem(m_list,i18n("Projects"));
150  } else m_projectRoot=0;
151  if (documents.count()>0) {
152  m_documentRoot=new TQListViewItem(m_list,i18n("Documents"));
153  const uint docCnt=documents.count();
154  for (uint i=0;i<docCnt;i++) {
155  new KateSaveModifiedDocumentCheckListItem(m_documentRoot,documents.at(i));
156  }
157  m_documentRoot->setOpen(true);
158  } else m_documentRoot=0;
159  connect(m_list, TQ_SIGNAL(clicked(TQListViewItem *)), TQ_SLOT(slotItemSelected()));
160  connect(m_list, TQ_SIGNAL(doubleClicked(TQListViewItem *)), TQ_SLOT(slotItemSelected()));
161  connect(m_list, TQ_SIGNAL(spacePressed(TQListViewItem *)), TQ_SLOT(slotItemSelected()));
162  if(documents.count()>3) { //For 3 or less, it would be quicker just to tick or untick them yourself, so don't clutter the gui.
163  connect(new TQPushButton(i18n("Se&lect All"),box),TQ_SIGNAL(clicked()),this,TQ_SLOT(slotSelectAll()));
164  }
165 }
166 
167 KateSaveModifiedDialog::~KateSaveModifiedDialog() {
168 }
169 
170 void KateSaveModifiedDialog::slotItemSelected() {
171  kdDebug(13001) << "slotItemSelected()" << endl;
172 
173  for(TQListViewItem *it=m_documentRoot->firstChild();it;it=it->nextSibling()) {
174  if(((TQCheckListItem*)it)->isOn()) {
175  enableButton(KDialogBase::Yes, true);
176  return;
177  }
178  }
179  enableButton(KDialogBase::Yes, false);
180 }
181 
182 static void selectItems(TQListViewItem *root) {
183  if (!root) return;
184  for (TQListViewItem *it=root->firstChild();it;it=it->nextSibling()) {
185  ((TQCheckListItem*)it)->setOn(true);
186  }
187 }
188 
189 void KateSaveModifiedDialog::slotSelectAll() {
190  selectItems(m_documentRoot);
191  slotItemSelected();
192 }
193 
194 
195 void KateSaveModifiedDialog::slotUser2() {
196  kdDebug(13001)<<"KateSaveModifiedDialog::slotYes()"<<endl;
197  if (doSave(m_documentRoot)) done(TQDialog::Accepted);
198 }
199 
200 void KateSaveModifiedDialog::slotUser1() {
201  done(TQDialog::Accepted);
202 }
203 
204 bool KateSaveModifiedDialog::doSave(TQListViewItem *root) {
205  if (root) {
206  for (TQListViewItem *it=root->firstChild();it;it=it->nextSibling()) {
207  AbstractKateSaveModifiedDialogCheckListItem *cit= (AbstractKateSaveModifiedDialogCheckListItem*)it;
208  if (cit->isOn() && (cit->state()!=AbstractKateSaveModifiedDialogCheckListItem::SaveOKState)) {
209  if (!cit->synchronousSave(this /*perhaps that should be the kate mainwindow*/)) {
210  KMessageBox::sorry( this, i18n("Data you requested to be saved could not be written. Please choose how you want to proceed."));
211  return false;
212  }
213  } else if ((!cit->isOn()) && (cit->state()==AbstractKateSaveModifiedDialogCheckListItem::SaveFailedState)) {
214  cit->setState(AbstractKateSaveModifiedDialogCheckListItem::InitialState);
215  }
216 
217  }
218  }
219  return true;
220 }
221 
222 bool KateSaveModifiedDialog::queryClose(TQWidget *parent,TQPtrList<Kate::Document> documents) {
223  KateSaveModifiedDialog d(parent,documents);
224  return (d.exec()!=TQDialog::Rejected);
225 }

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kate

Skip menu "kate"
  • kate
  • libkonq
  • twin
  •   lib
Generated for kate by doxygen 1.8.13
This website is maintained by Timothy Pearson.