katesavemodifieddialog.cpp
00001 /* This file is part of the KDE project 00002 Copyright (C) 2004 Joseph Wenninger <jowenn@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "katesavemodifieddialog.h" 00020 #include "katesavemodifieddialog.moc" 00021 00022 #include <tdelocale.h> 00023 #include <tqlistview.h> 00024 #include <tdelistview.h> 00025 #include <kguiitem.h> 00026 #include <kactivelabel.h> 00027 #include <kstdguiitem.h> 00028 #include <tqvbox.h> 00029 #include <tqlabel.h> 00030 #include <tqpushbutton.h> 00031 #include <kiconloader.h> 00032 #include <tdemessagebox.h> 00033 #include <kdebug.h> 00034 #include <kencodingfiledialog.h> 00035 #include <tdetexteditor/encodinginterface.h> 00036 00037 class AbstractKateSaveModifiedDialogCheckListItem:public TQCheckListItem { 00038 public: 00039 AbstractKateSaveModifiedDialogCheckListItem(TQListViewItem *parent,const TQString& title, const TQString& url):TQCheckListItem(parent,title,TQCheckListItem::CheckBox) { 00040 setText(1,url); 00041 setOn(true); 00042 setState(InitialState); 00043 } 00044 virtual ~AbstractKateSaveModifiedDialogCheckListItem() { 00045 } 00046 virtual bool synchronousSave(TQWidget *dialogParent)=0; 00047 enum STATE{InitialState,SaveOKState,SaveFailedState}; 00048 STATE state() const { return m_state;} 00049 void setState(enum STATE state) { 00050 m_state=state; 00051 TDEIconLoader *loader = TDEGlobal::instance()->iconLoader(); 00052 switch (state) { 00053 case InitialState: 00054 setPixmap(0,TQPixmap()); 00055 break; 00056 case SaveOKState: 00057 setPixmap(0,loader->loadIcon("ok",TDEIcon::NoGroup,height())); 00058 break; 00059 case SaveFailedState: 00060 setPixmap(0,loader->loadIcon("cancel",TDEIcon::NoGroup,height())); 00061 break; 00062 } 00063 } 00064 private: 00065 STATE m_state; 00066 }; 00067 00068 class KateSaveModifiedDocumentCheckListItem:public AbstractKateSaveModifiedDialogCheckListItem { 00069 public: 00070 KateSaveModifiedDocumentCheckListItem(TQListViewItem *parent,Kate::Document *document):AbstractKateSaveModifiedDialogCheckListItem(parent,document->docName(),document->url().prettyURL()){ 00071 m_document=document; 00072 } 00073 virtual ~KateSaveModifiedDocumentCheckListItem() { 00074 } 00075 virtual bool synchronousSave(TQWidget *dialogParent) { 00076 if (m_document->url().isEmpty() ) { 00077 KEncodingFileDialog::Result r=KEncodingFileDialog::getSaveURLAndEncoding( 00078 KTextEditor::encodingInterface(m_document)->encoding(),TQString::null,TQString::null,dialogParent,i18n("Save As (%1)").arg(m_document->docName())); 00079 00080 m_document->setEncoding( r.encoding ); 00081 if (!r.URLs.isEmpty()) { 00082 KURL tmp = r.URLs.first(); 00083 if ( !m_document->saveAs( tmp ) ) { 00084 setState(SaveFailedState); 00085 setText(1,m_document->url().prettyURL()); 00086 return false; 00087 } else { 00088 bool sc=m_document->waitSaveComplete(); 00089 setText(1,m_document->url().prettyURL()); 00090 if (!sc) { 00091 setState(SaveFailedState); 00092 return false; 00093 } else { 00094 setState(SaveOKState); 00095 return true; 00096 } 00097 } 00098 } else { 00099 setState(SaveFailedState); 00100 return false; 00101 } 00102 } else { //document has an exising location 00103 if ( !m_document->save() ) { 00104 setState(SaveFailedState); 00105 setText(1,m_document->url().prettyURL()); 00106 return false; 00107 } else { 00108 bool sc=m_document->waitSaveComplete(); 00109 setText(1,m_document->url().prettyURL()); 00110 if (!sc) { 00111 setState(SaveFailedState); 00112 return false; 00113 } else { 00114 setState(SaveOKState); 00115 return true; 00116 } 00117 } 00118 00119 } 00120 00121 return false; 00122 00123 } 00124 private: 00125 Kate::Document *m_document; 00126 }; 00127 00128 KateSaveModifiedDialog::KateSaveModifiedDialog(TQWidget *parent, TQPtrList<Kate::Document> documents): 00129 KDialogBase( parent, "KateSaveModifiedDialog", true, i18n("Save Documents"), Yes | No | Cancel) { 00130 00131 KGuiItem saveItem=KStdGuiItem::save(); 00132 saveItem.setText(i18n("&Save Selected")); 00133 setButtonGuiItem(KDialogBase::Yes,saveItem); 00134 00135 setButtonGuiItem(KDialogBase::No,KStdGuiItem::dontSave()); 00136 00137 KGuiItem cancelItem=KStdGuiItem::cancel(); 00138 cancelItem.setText(i18n("&Abort Closing")); 00139 setButtonGuiItem(KDialogBase::Cancel,cancelItem); 00140 00141 TQVBox *box=makeVBoxMainWidget(); 00142 new KActiveLabel(i18n("<qt>The following documents have been modified. Do you want to save them before closing?</qt>"),box); 00143 m_list=new TDEListView(box); 00144 m_list->addColumn(i18n("Title")); 00145 m_list->addColumn(i18n("Location")); 00146 m_list->setRootIsDecorated(true); 00147 m_list->setResizeMode(TQListView::LastColumn); 00148 if (0) { 00149 m_projectRoot=new TQListViewItem(m_list,i18n("Projects")); 00150 } else m_projectRoot=0; 00151 if (documents.count()>0) { 00152 m_documentRoot=new TQListViewItem(m_list,i18n("Documents")); 00153 const uint docCnt=documents.count(); 00154 for (uint i=0;i<docCnt;i++) { 00155 new KateSaveModifiedDocumentCheckListItem(m_documentRoot,documents.at(i)); 00156 } 00157 m_documentRoot->setOpen(true); 00158 } else m_documentRoot=0; 00159 //FIXME - Is this the best way? 00160 connect(m_list, TQT_SIGNAL(clicked(TQListViewItem *)), TQT_SLOT(slotItemSelected())); 00161 connect(m_list, TQT_SIGNAL(doubleClicked(TQListViewItem *)), TQT_SLOT(slotItemSelected())); 00162 connect(m_list, TQT_SIGNAL(spacePressed(TQListViewItem *)), TQT_SLOT(slotItemSelected())); 00163 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. 00164 connect(new TQPushButton(i18n("Se&lect All"),box),TQT_SIGNAL(clicked()),this,TQT_SLOT(slotSelectAll())); 00165 } 00166 } 00167 00168 KateSaveModifiedDialog::~KateSaveModifiedDialog() { 00169 } 00170 00171 void KateSaveModifiedDialog::slotItemSelected() { 00172 kdDebug(13001) << "slotItemSelected()" << endl; 00173 00174 for(TQListViewItem *it=m_documentRoot->firstChild();it;it=it->nextSibling()) { 00175 if(((TQCheckListItem*)it)->isOn()) { 00176 enableButton(KDialogBase::Yes, true); 00177 return; 00178 } 00179 } 00180 enableButton(KDialogBase::Yes, false); 00181 } 00182 00183 static void selectItems(TQListViewItem *root) { 00184 if (!root) return; 00185 for (TQListViewItem *it=root->firstChild();it;it=it->nextSibling()) { 00186 ((TQCheckListItem*)it)->setOn(true); 00187 } 00188 } 00189 00190 void KateSaveModifiedDialog::slotSelectAll() { 00191 selectItems(m_documentRoot); 00192 slotItemSelected(); 00193 } 00194 00195 00196 void KateSaveModifiedDialog::slotUser2() { 00197 kdDebug(13001)<<"KateSaveModifiedDialog::slotYes()"<<endl; 00198 if (doSave(m_documentRoot)) done(TQDialog::Accepted); 00199 } 00200 00201 void KateSaveModifiedDialog::slotUser1() { 00202 done(TQDialog::Accepted); 00203 } 00204 00205 bool KateSaveModifiedDialog::doSave(TQListViewItem *root) { 00206 if (root) { 00207 for (TQListViewItem *it=root->firstChild();it;it=it->nextSibling()) { 00208 AbstractKateSaveModifiedDialogCheckListItem *cit= (AbstractKateSaveModifiedDialogCheckListItem*)it; 00209 if (cit->isOn() && (cit->state()!=AbstractKateSaveModifiedDialogCheckListItem::SaveOKState)) { 00210 if (!cit->synchronousSave(this /*perhaps that should be the kate mainwindow*/)) { 00211 KMessageBox::sorry( this, i18n("Data you requested to be saved could not be written. Please choose how you want to proceed.")); 00212 return false; 00213 } 00214 } else if ((!cit->isOn()) && (cit->state()==AbstractKateSaveModifiedDialogCheckListItem::SaveFailedState)) { 00215 cit->setState(AbstractKateSaveModifiedDialogCheckListItem::InitialState); 00216 } 00217 00218 } 00219 } 00220 return true; 00221 } 00222 00223 bool KateSaveModifiedDialog::queryClose(TQWidget *parent,TQPtrList<Kate::Document> documents) { 00224 KateSaveModifiedDialog d(parent,documents); 00225 return (d.exec()!=TQDialog::Rejected); 00226 }