19 #include "katesavemodifieddialog.h"
20 #include "katesavemodifieddialog.moc"
22 #include <tdelocale.h>
23 #include <tqlistview.h>
24 #include <tdelistview.h>
26 #include <kactivelabel.h>
27 #include <kstdguiitem.h>
30 #include <tqpushbutton.h>
31 #include <kiconloader.h>
32 #include <tdemessagebox.h>
34 #include <kencodingfiledialog.h>
35 #include <tdetexteditor/encodinginterface.h>
37 class AbstractKateSaveModifiedDialogCheckListItem:
public TQCheckListItem {
39 AbstractKateSaveModifiedDialogCheckListItem(TQListViewItem *parent,
const TQString& title,
const TQString& url):TQCheckListItem(parent,title,TQCheckListItem::CheckBox) {
42 setState(InitialState);
44 virtual ~AbstractKateSaveModifiedDialogCheckListItem() {
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) {
51 TDEIconLoader *loader = TDEGlobal::instance()->iconLoader();
54 setPixmap(0,TQPixmap());
57 setPixmap(0,loader->loadIcon(
"ok",TDEIcon::NoGroup,height()));
60 setPixmap(0,loader->loadIcon(
"cancel",TDEIcon::NoGroup,height()));
68 class KateSaveModifiedDocumentCheckListItem:
public AbstractKateSaveModifiedDialogCheckListItem {
70 KateSaveModifiedDocumentCheckListItem(TQListViewItem *parent,Kate::Document *document):AbstractKateSaveModifiedDialogCheckListItem(parent,document->docName(),document->url().prettyURL()){
73 virtual ~KateSaveModifiedDocumentCheckListItem() {
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()));
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());
88 bool sc=m_document->waitSaveComplete();
89 setText(1,m_document->url().prettyURL());
91 setState(SaveFailedState);
94 setState(SaveOKState);
99 setState(SaveFailedState);
103 if ( !m_document->save() ) {
104 setState(SaveFailedState);
105 setText(1,m_document->url().prettyURL());
108 bool sc=m_document->waitSaveComplete();
109 setText(1,m_document->url().prettyURL());
111 setState(SaveFailedState);
114 setState(SaveOKState);
125 Kate::Document *m_document;
128 KateSaveModifiedDialog::KateSaveModifiedDialog(TQWidget *parent, TQPtrList<Kate::Document> documents):
129 KDialogBase( parent,
"KateSaveModifiedDialog", true, i18n(
"Save Documents"), Yes | No | Cancel) {
131 KGuiItem saveItem=KStdGuiItem::save();
132 saveItem.setText(i18n(
"&Save Selected"));
133 setButtonGuiItem(KDialogBase::Yes,saveItem);
135 setButtonGuiItem(KDialogBase::No,KStdGuiItem::dontSave());
137 KGuiItem cancelItem=KStdGuiItem::cancel();
138 cancelItem.setText(i18n(
"&Abort Closing"));
139 setButtonGuiItem(KDialogBase::Cancel,cancelItem);
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);
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));
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) {
163 connect(
new TQPushButton(i18n(
"Se&lect All"),box),TQ_SIGNAL(clicked()),
this,TQ_SLOT(slotSelectAll()));
167 KateSaveModifiedDialog::~KateSaveModifiedDialog() {
170 void KateSaveModifiedDialog::slotItemSelected() {
171 kdDebug(13001) <<
"slotItemSelected()" << endl;
173 for(TQListViewItem *it=m_documentRoot->firstChild();it;it=it->nextSibling()) {
174 if(((TQCheckListItem*)it)->isOn()) {
175 enableButton(KDialogBase::Yes,
true);
179 enableButton(KDialogBase::Yes,
false);
182 static void selectItems(TQListViewItem *root) {
184 for (TQListViewItem *it=root->firstChild();it;it=it->nextSibling()) {
185 ((TQCheckListItem*)it)->setOn(
true);
189 void KateSaveModifiedDialog::slotSelectAll() {
190 selectItems(m_documentRoot);
195 void KateSaveModifiedDialog::slotUser2() {
196 kdDebug(13001)<<
"KateSaveModifiedDialog::slotYes()"<<endl;
197 if (doSave(m_documentRoot)) done(TQDialog::Accepted);
200 void KateSaveModifiedDialog::slotUser1() {
201 done(TQDialog::Accepted);
204 bool KateSaveModifiedDialog::doSave(TQListViewItem *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 )) {
210 KMessageBox::sorry(
this, i18n(
"Data you requested to be saved could not be written. Please choose how you want to proceed."));
213 }
else if ((!cit->isOn()) && (cit->state()==AbstractKateSaveModifiedDialogCheckListItem::SaveFailedState)) {
214 cit->setState(AbstractKateSaveModifiedDialogCheckListItem::InitialState);
222 bool KateSaveModifiedDialog::queryClose(TQWidget *parent,TQPtrList<Kate::Document> documents) {
223 KateSaveModifiedDialog d(parent,documents);
224 return (d.exec()!=TQDialog::Rejected);