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;
160 connect(m_list, TQT_SIGNAL(clicked(TQListViewItem *)), TQT_SLOT(slotItemSelected()));
161 connect(m_list, TQT_SIGNAL(doubleClicked(TQListViewItem *)), TQT_SLOT(slotItemSelected()));
162 connect(m_list, TQT_SIGNAL(spacePressed(TQListViewItem *)), TQT_SLOT(slotItemSelected()));
163 if(documents.count()>3) {
164 connect(
new TQPushButton(i18n(
"Se&lect All"),box),TQT_SIGNAL(clicked()),
this,TQT_SLOT(slotSelectAll()));
168 KateSaveModifiedDialog::~KateSaveModifiedDialog() {
171 void KateSaveModifiedDialog::slotItemSelected() {
172 kdDebug(13001) <<
"slotItemSelected()" << endl;
174 for(TQListViewItem *it=m_documentRoot->firstChild();it;it=it->nextSibling()) {
175 if(((TQCheckListItem*)it)->isOn()) {
176 enableButton(KDialogBase::Yes,
true);
180 enableButton(KDialogBase::Yes,
false);
183 static void selectItems(TQListViewItem *root) {
185 for (TQListViewItem *it=root->firstChild();it;it=it->nextSibling()) {
186 ((TQCheckListItem*)it)->setOn(
true);
190 void KateSaveModifiedDialog::slotSelectAll() {
191 selectItems(m_documentRoot);
196 void KateSaveModifiedDialog::slotUser2() {
197 kdDebug(13001)<<
"KateSaveModifiedDialog::slotYes()"<<endl;
198 if (doSave(m_documentRoot)) done(TQDialog::Accepted);
201 void KateSaveModifiedDialog::slotUser1() {
202 done(TQDialog::Accepted);
205 bool KateSaveModifiedDialog::doSave(TQListViewItem *root) {
207 for (TQListViewItem *it=root->firstChild();it;it=it->nextSibling()) {
208 AbstractKateSaveModifiedDialogCheckListItem *cit= (AbstractKateSaveModifiedDialogCheckListItem*)it;
209 if (cit->isOn() && (cit->state()!=AbstractKateSaveModifiedDialogCheckListItem::SaveOKState)) {
210 if (!cit->synchronousSave(
this )) {
211 KMessageBox::sorry(
this, i18n(
"Data you requested to be saved could not be written. Please choose how you want to proceed."));
214 }
else if ((!cit->isOn()) && (cit->state()==AbstractKateSaveModifiedDialogCheckListItem::SaveFailedState)) {
215 cit->setState(AbstractKateSaveModifiedDialogCheckListItem::InitialState);
223 bool KateSaveModifiedDialog::queryClose(TQWidget *parent,TQPtrList<Kate::Document> documents) {
224 KateSaveModifiedDialog d(parent,documents);
225 return (d.exec()!=TQDialog::Rejected);