kate Library API Documentation

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., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "katesavemodifieddialog.h"
00020 #include "katesavemodifieddialog.moc"
00021 
00022 #include <klocale.h>
00023 #include <qlistview.h>
00024 #include <klistview.h>
00025 #include <kguiitem.h>
00026 #include <kactivelabel.h>
00027 #include <kstdguiitem.h>
00028 #include <qvbox.h>
00029 #include <qlabel.h>
00030 #include <qpushbutton.h>
00031 #include <kiconloader.h>
00032 #include <kmessagebox.h>
00033 #include <kdebug.h>
00034 #include <kencodingfiledialog.h>
00035 #include <ktexteditor/encodinginterface.h>
00036 
00037 class AbstractKateSaveModifiedDialogCheckListItem:public QCheckListItem {
00038 public:
00039         AbstractKateSaveModifiedDialogCheckListItem(QListViewItem *parent,const QString& title, const QString& url):QCheckListItem(parent,title,QCheckListItem::CheckBox) {
00040                 setText(1,url);
00041                 setOn(true);
00042         setState(InitialState);
00043         }
00044         virtual ~AbstractKateSaveModifiedDialogCheckListItem() {
00045         }
00046         virtual bool synchronousSave(QWidget *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         KIconLoader *loader = KGlobal::instance()->iconLoader();
00052         switch (state) {
00053             case InitialState:
00054                 setPixmap(0,QPixmap());
00055                 break;
00056             case SaveOKState:
00057                 setPixmap(0,loader->loadIcon("ok",KIcon::NoGroup,height()));
00058                 break;
00059             case SaveFailedState:
00060                 setPixmap(0,loader->loadIcon("cancel",KIcon::NoGroup,height()));
00061                 break;
00062         }
00063     }
00064 private:
00065     STATE m_state;
00066 };
00067 
00068 class KateSaveModifiedDocumentCheckListItem:public AbstractKateSaveModifiedDialogCheckListItem {
00069 public:
00070     KateSaveModifiedDocumentCheckListItem(QListViewItem *parent,Kate::Document *document):AbstractKateSaveModifiedDialogCheckListItem(parent,document->docName(),document->url().prettyURL()){
00071         m_document=document;
00072     }
00073     virtual ~KateSaveModifiedDocumentCheckListItem() {
00074     }
00075     virtual bool synchronousSave(QWidget *dialogParent) {
00076         if (m_document->url().isEmpty() ) {
00077                 KEncodingFileDialog::Result r=KEncodingFileDialog::getSaveURLAndEncoding(
00078                     KTextEditor::encodingInterface(m_document)->encoding(),QString::null,QString::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(QWidget *parent, QPtrList<Kate::Document> documents):
00129     KDialogBase( parent, "KateSaveModifiedDialog", true, i18n("Save Documents"), Yes | No | Cancel) {
00130 
00131     KGuiItem yesItem=KStdGuiItem::yes();
00132     yesItem.setText(i18n("&Save Selected"));
00133     setButtonGuiItem(KDialogBase::Yes,yesItem);
00134 
00135     KGuiItem noItem=KStdGuiItem::dontSave();
00136     setButtonGuiItem(KDialogBase::No,noItem);
00137 
00138     KGuiItem cancelItem=KStdGuiItem::close();
00139     cancelItem.setText(i18n("&Abort Closing"));
00140     setButtonGuiItem(KDialogBase::Cancel,cancelItem);
00141 
00142     QVBox *box=makeVBoxMainWidget();
00143     new KActiveLabel(i18n("<qt>The following documents have been modified. Do you want to save them before closing?</qt>"),box);
00144     m_list=new KListView(box);
00145     m_list->addColumn(i18n("Title"));
00146     m_list->addColumn(i18n("Location"));
00147     m_list->setRootIsDecorated(true);
00148     m_list->setResizeMode(QListView::LastColumn);
00149     if (0) {
00150         m_projectRoot=new QListViewItem(m_list,i18n("Projects"));
00151     } else m_projectRoot=0;
00152     if (documents.count()>0) {
00153         m_documentRoot=new QListViewItem(m_list,i18n("Documents"));
00154         const uint docCnt=documents.count();
00155         for (uint i=0;i<docCnt;i++) {
00156             new KateSaveModifiedDocumentCheckListItem(m_documentRoot,documents.at(i));
00157         }
00158         m_documentRoot->setOpen(true);
00159     } else m_documentRoot=0;
00160     //FIXME - Is this the best way?
00161     connect(m_list, SIGNAL(clicked(QListViewItem *)), SLOT(slotItemSelected()));
00162     connect(m_list, SIGNAL(doubleclicked(QListViewItem *)), SLOT(slotItemSelected()));
00163     connect(m_list, SIGNAL(spacePressed(QListViewItem *)), SLOT(slotItemSelected()));
00164     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.
00165         connect(new QPushButton(i18n("Se&lect All"),box),SIGNAL(clicked()),this,SLOT(slotSelectAll()));
00166     }
00167 }
00168 
00169 KateSaveModifiedDialog::~KateSaveModifiedDialog() {
00170 }
00171 
00172 void KateSaveModifiedDialog::slotItemSelected() {
00173     kdDebug(13001) << "slotItemSelected()" << endl;
00174 
00175     for(QListViewItem *it=m_documentRoot->firstChild();it;it=it->nextSibling()) {
00176         if(((QCheckListItem*)it)->isOn()) {
00177             enableButton(KDialogBase::Yes, true);
00178             return;
00179         }
00180     }
00181     enableButton(KDialogBase::Yes, false);
00182 }
00183 
00184 static void selectItems(QListViewItem *root) {
00185     if (!root) return;
00186     for (QListViewItem *it=root->firstChild();it;it=it->nextSibling()) {
00187         ((QCheckListItem*)it)->setOn(true);
00188     }
00189 }
00190 
00191 void KateSaveModifiedDialog::slotSelectAll() {
00192     selectItems(m_documentRoot);
00193     slotItemSelected();
00194 }
00195 
00196 
00197 void KateSaveModifiedDialog::slotUser2() {
00198     kdDebug(13001)<<"KateSaveModifiedDialog::slotYes()"<<endl;
00199     if (doSave(m_documentRoot)) done(QDialog::Accepted);
00200 }
00201 
00202 void KateSaveModifiedDialog::slotUser1() {
00203     done(QDialog::Accepted);
00204 }
00205 
00206 bool KateSaveModifiedDialog::doSave(QListViewItem *root) {
00207     if (root) {
00208         for (QListViewItem *it=root->firstChild();it;it=it->nextSibling()) {
00209             AbstractKateSaveModifiedDialogCheckListItem *cit= (AbstractKateSaveModifiedDialogCheckListItem*)it;
00210             if (cit->isOn() && (cit->state()!=AbstractKateSaveModifiedDialogCheckListItem::SaveOKState)) {
00211                 if (!cit->synchronousSave(this /*perhaps that should be the kate mainwindow*/)) {
00212                     KMessageBox::sorry( this, i18n("Data you requested to be saved could not be written. Please choose how you want to proceed."));
00213                     return false;
00214                 }
00215             } else if ((!cit->isOn()) && (cit->state()==AbstractKateSaveModifiedDialogCheckListItem::SaveFailedState)) {
00216                 cit->setState(AbstractKateSaveModifiedDialogCheckListItem::InitialState);
00217             }
00218 
00219         }
00220     }
00221     return true;
00222 }
00223 
00224 bool KateSaveModifiedDialog::queryClose(QWidget *parent,QPtrList<Kate::Document> documents) {
00225     KateSaveModifiedDialog d(parent,documents);
00226     return (d.exec()!=QDialog::Rejected);
00227 }
KDE Logo
This file is part of the documentation for kate Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Apr 6 02:40:58 2005 by doxygen 1.4.0 written by Dimitri van Heesch, © 1997-2003