21 #include "noteshared/attributes/showfoldernotesattribute.h"
22 #include <Akonadi/Notes/NoteUtils>
23 #include "notesharedglobalconfig.h"
24 #include "pimcommon/widgets/manageaccountwidget.h"
26 #include <Akonadi/CollectionModifyJob>
27 #include <Akonadi/CollectionFilterProxyModel>
28 #include <KRecursiveFilterProxyModel>
29 #include <KInputDialog>
31 #include <Akonadi/EntityTreeView>
32 #include <Akonadi/CollectionRequester>
33 #include <Akonadi/ChangeRecorder>
34 #include <Akonadi/EntityTreeModel>
35 #include <Akonadi/Collection>
36 #include <Akonadi/EntityDisplayAttribute>
37 #include <Akonadi/CollectionModifyJob>
38 #include <KMime/Message>
40 #include <KCheckableProxyModel>
42 #include <KLocalizedString>
43 #include <KPushButton>
46 #include <KMessageBox>
48 #include <QVBoxLayout>
49 #include <QHBoxLayout>
56 mCanUpdateStatus(false)
67 tabWidget->
addTab(collectionWidget, i18n(
"Folders"));
69 QLabel *label =
new QLabel(i18n(
"Select which KNotes folders to show:"));
73 mChangeRecorder =
new Akonadi::ChangeRecorder(
this );
74 mChangeRecorder->setMimeTypeMonitored( Akonadi::NoteUtils::noteMimeType() );
75 mChangeRecorder->fetchCollection(
true );
76 mChangeRecorder->setAllMonitored(
true );
78 mModel =
new Akonadi::EntityTreeModel( mChangeRecorder,
this );
80 mModel->setItemPopulationStrategy( Akonadi::EntityTreeModel::NoItemPopulation );
82 Akonadi::CollectionFilterProxyModel *mimeTypeProxy =
new Akonadi::CollectionFilterProxyModel(
this );
83 mimeTypeProxy->setExcludeVirtualCollections(
true );
84 mimeTypeProxy->addMimeTypeFilters(
QStringList() << Akonadi::NoteUtils::noteMimeType() );
85 mimeTypeProxy->setSourceModel( mModel );
89 mCheckProxy =
new KCheckableProxyModel(
this );
90 mCheckProxy->setSelectionModel( mSelectionModel );
91 mCheckProxy->setSourceModel( mimeTypeProxy );
94 this, SLOT(slotCollectionsInserted(
QModelIndex,
int,
int)));
97 mCollectionFilter =
new KRecursiveFilterProxyModel(
this);
98 mCollectionFilter->setSourceModel(mCheckProxy);
99 mCollectionFilter->setDynamicSortFilter(
true);
100 mCollectionFilter->setFilterCaseSensitivity(Qt::CaseInsensitive);
103 KLineEdit *searchLine =
new KLineEdit(
this);
104 searchLine->setPlaceholderText(i18n(
"Search..."));
105 searchLine->setClearButtonShown(
true);
107 this, SLOT(slotSetCollectionFilter(
QString)));
111 mFolderView =
new Akonadi::EntityTreeView(
this);
112 mFolderView->setDragEnabled(
false);
113 mFolderView->setEditTriggers(QAbstractItemView::NoEditTriggers);
114 mFolderView->setAlternatingRowColors(
true);
117 mFolderView->setModel( mCollectionFilter );
123 KPushButton *button =
new KPushButton(i18n(
"&Select All"),
this);
124 connect(button, SIGNAL(clicked(
bool)),
this, SLOT(slotSelectAllCollections()));
125 hbox->addWidget(button);
127 button =
new KPushButton(i18n(
"&Unselect All"),
this);
128 connect(button, SIGNAL(clicked(
bool)),
this, SLOT(slotUnselectAllCollections()));
129 hbox->addWidget(button);
132 mRenameCollection =
new KPushButton(i18n(
"Rename notes..."),
this);
133 connect(mRenameCollection, SIGNAL(clicked(
bool)),
this, SLOT(slotRenameCollection()));
134 hbox->addWidget(mRenameCollection);
137 vbox->
addWidget(
new QLabel(i18nc(
"@info",
"Select the folder where the note will be saved:" )));
138 mDefaultSaveFolder =
new Akonadi::CollectionRequester(Akonadi::Collection(NoteShared::NoteSharedGlobalConfig::self()->defaultFolder()));
139 mDefaultSaveFolder->setMimeTypeFilter(
QStringList() << Akonadi::NoteUtils::noteMimeType());
143 Akonadi::CollectionDialog::CollectionDialogOptions options;
144 options |= Akonadi::CollectionDialog::AllowToCreateNewChildCollection;
145 options |= Akonadi::CollectionDialog::KeepTreeExpanded;
146 mDefaultSaveFolder->changeCollectionDialogOptions(options);
147 connect(mDefaultSaveFolder, SIGNAL(collectionChanged(Akonadi::Collection)),
this, SLOT(slotDataChanged()));
154 accountWidget->
setLayout(vboxAccountWidget);
156 PimCommon::ManageAccountWidget *manageAccountWidget =
new PimCommon::ManageAccountWidget(
this);
157 vboxAccountWidget->
addWidget(manageAccountWidget);
159 manageAccountWidget->setMimeTypeFilter(
QStringList() << Akonadi::NoteUtils::noteMimeType());
161 tabWidget->
addTab(accountWidget, i18n(
"Accounts"));
172 void KNoteCollectionConfigWidget::slotUpdateButtons()
174 mRenameCollection->setEnabled(mFolderView->selectionModel()->hasSelection());
177 void KNoteCollectionConfigWidget::slotRenameCollection()
179 const QModelIndexList rows = mFolderView->selectionModel()->selectedRows();
181 if ( rows.size() != 1 )
188 Akonadi::Collection col = idx.
data( Akonadi::EntityTreeModel::CollectionRole ).
value<Akonadi::Collection>();
189 Q_ASSERT( col.isValid() );
194 const QString name = KInputDialog::getText( i18n(
"Rename Notes" ),
195 i18n(
"Name:" ), title, &ok,
this );
198 if ( col.hasAttribute<Akonadi::EntityDisplayAttribute>() &&
199 !col.attribute<Akonadi::EntityDisplayAttribute>()->displayName().isEmpty() ) {
200 col.attribute<Akonadi::EntityDisplayAttribute>()->setDisplayName( name );
201 }
else if ( !name.
isEmpty() ) {
205 Akonadi::CollectionModifyJob *job =
new Akonadi::CollectionModifyJob(col,
this);
206 connect( job, SIGNAL(result(KJob*)), SLOT(slotCollectionModifyFinished(KJob*)) );
211 void KNoteCollectionConfigWidget::slotCollectionModifyFinished(KJob *job)
214 KMessageBox::error(
this, i18n(
"An error was occurred during renaming: %1", job->errorString()), i18n(
"Rename note"));
218 void KNoteCollectionConfigWidget::slotDataChanged()
223 void KNoteCollectionConfigWidget::slotSetCollectionFilter(
const QString &filter)
225 mCollectionFilter->setFilterWildcard(filter);
226 mFolderView->expandAll();
229 void KNoteCollectionConfigWidget::slotUpdateCollectionStatus()
231 mCanUpdateStatus =
true;
235 void KNoteCollectionConfigWidget::slotSelectAllCollections()
241 void KNoteCollectionConfigWidget::slotUnselectAllCollections()
247 void KNoteCollectionConfigWidget::updateStatus(
const QModelIndex &parent)
249 if (!mCanUpdateStatus)
252 const int nbCol = mCheckProxy->rowCount( parent );
253 for (
int i = 0; i < nbCol; ++i ) {
256 const Akonadi::Collection collection =
257 mCheckProxy->data( child, Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
259 NoteShared::ShowFolderNotesAttribute *attr = collection.attribute<NoteShared::ShowFolderNotesAttribute>();
261 mCheckProxy->setData( child, Qt::Checked, Qt::CheckStateRole );
263 updateStatus( child );
267 void KNoteCollectionConfigWidget::forceStatus(
const QModelIndex &parent,
bool status)
269 const int nbCol = mCheckProxy->rowCount( parent );
270 for (
int i = 0; i < nbCol; ++i ) {
271 const QModelIndex child = mCheckProxy->index( i, 0, parent );
272 mCheckProxy->setData( child, status ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole );
273 forceStatus( child, status );
277 void KNoteCollectionConfigWidget::slotCollectionsInserted(
const QModelIndex &,
int,
int)
279 mFolderView->expandAll();
285 Akonadi::Collection col = mDefaultSaveFolder->collection();
287 NoteShared::NoteSharedGlobalConfig::self()->setDefaultFolder(col.id());
288 NoteShared::NoteSharedGlobalConfig::self()->writeConfig();
294 const int nbCol = mCheckProxy->rowCount( parent );
295 for (
int i = 0; i < nbCol; ++i ) {
296 const QModelIndex child = mCheckProxy->index( i, 0, parent );
298 Akonadi::Collection collection =
299 mCheckProxy->data( child, Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
301 NoteShared::ShowFolderNotesAttribute *attr = collection.attribute<NoteShared::ShowFolderNotesAttribute>();
302 Akonadi::CollectionModifyJob *modifyJob = 0;
303 const bool selected = (mCheckProxy->data( child, Qt::CheckStateRole ).value<
int>() != 0);
304 if (selected && !attr) {
305 attr = collection.attribute<NoteShared::ShowFolderNotesAttribute>(Akonadi::Entity::AddIfMissing);
306 modifyJob =
new Akonadi::CollectionModifyJob(collection);
307 modifyJob->setProperty(
"AttributeAdded",
true);
308 }
else if (!selected && attr) {
309 collection.removeAttribute<NoteShared::ShowFolderNotesAttribute>();
310 modifyJob =
new Akonadi::CollectionModifyJob(collection);
311 modifyJob->setProperty(
"AttributeAdded",
false);
315 connect(modifyJob, SIGNAL(finished(KJob*)), SLOT(slotModifyJobDone(KJob*)));
321 void KNoteCollectionConfigWidget::slotModifyJobDone(KJob* job)
323 Akonadi::CollectionModifyJob *modifyJob = qobject_cast<Akonadi::CollectionModifyJob*>(job);
324 if (modifyJob && job->error()) {
325 if (job->property(
"AttributeAdded").toBool()) {
326 kWarning() <<
"Failed to append ShowFolderNotesAttribute to collection"
327 << modifyJob->collection().id() <<
":"
328 << job->errorString();
330 kWarning() <<
"Failed to remove ShowFolderNotesAttribute from collection"
331 << modifyJob->collection().id() <<
":"
332 << job->errorString();
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
const char * name() const
QVariant data(int role) const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void addLayout(QLayout *layout, int stretch)