1 | /* |
2 | Copyright (c) 2009 Volker Krause <vkrause@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "typepage.h" |
21 | |
22 | #include <KDebug> |
23 | #include <KDesktopFile> |
24 | #include <KStandardDirs> |
25 | |
26 | #include <QSortFilterProxyModel> |
27 | #include "global.h" |
28 | #include <kmimetype.h> |
29 | |
30 | TypePage::TypePage(KAssistantDialog* parent) : |
31 | Page( parent ), |
32 | m_model( new QStandardItemModel( this ) ) |
33 | { |
34 | ui.setupUi( this ); |
35 | #ifdef KDEPIM_MOBILE_UI |
36 | ui.label->setHidden( true ); |
37 | ui.searchLine->setHidden( true ); |
38 | #endif |
39 | |
40 | #ifdef ACCOUNTWIZARD_NO_GHNS |
41 | ui.ghnsButton->hide(); |
42 | #endif |
43 | |
44 | QSortFilterProxyModel *proxy = new QSortFilterProxyModel( this ); |
45 | proxy->setSourceModel( m_model ); |
46 | ui.listView->setModel( proxy ); |
47 | ui.searchLine->setProxy( proxy ); |
48 | |
49 | const QStringList list = KGlobal::dirs()->findAllResources( "data" , QLatin1String( "akonadi/accountwizard/*.desktop" ), |
50 | KStandardDirs::Recursive | KStandardDirs::NoDuplicates ); |
51 | const QStringList filter = Global::typeFilter(); |
52 | foreach ( const QString &entry, list ) { |
53 | KDesktopFile f( entry ); |
54 | kDebug() << entry << f.readName(); |
55 | const KConfig configWizard( entry ); |
56 | KConfigGroup grp( &configWizard, "Wizard" ); |
57 | const QStringList lstType = grp.readEntry( "Type" , QStringList() ); |
58 | if ( lstType.isEmpty() ) { |
59 | kDebug() << QString::fromLatin1( " %1 doesn't contains specific type" ).arg( f.readName() ); |
60 | } |
61 | if ( !filter.isEmpty() ) { |
62 | // stolen from agentfilterproxymodel |
63 | bool found = false; |
64 | foreach ( const QString &mimeType, lstType ) { |
65 | if ( filter.contains( mimeType ) ) { |
66 | found = true; |
67 | break; |
68 | } else { |
69 | foreach ( const QString &type, filter ) { |
70 | KMimeType::Ptr typePtr = KMimeType::mimeType( type, KMimeType::ResolveAliases ); |
71 | if ( !typePtr.isNull() && typePtr->is( mimeType ) ) { |
72 | found = true; |
73 | break; |
74 | } |
75 | } |
76 | } |
77 | if ( found ) |
78 | break; |
79 | } |
80 | if ( !found ) |
81 | continue; |
82 | } |
83 | QStandardItem *item = new QStandardItem( f.readName() ); |
84 | item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled ); |
85 | item->setData( entry, Qt::UserRole ); |
86 | if ( !f.readIcon().isEmpty() ) |
87 | item->setData( KIcon( f.readIcon() ), Qt::DecorationRole ); |
88 | item->setData( f.readComment(), Qt::ToolTipRole ); |
89 | m_model->appendRow( item ); |
90 | } |
91 | |
92 | connect( ui.listView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(selectionChanged()) ); |
93 | connect( ui.ghnsButton, SIGNAL(clicked()), SIGNAL(ghnsWanted()) ); |
94 | } |
95 | |
96 | void TypePage::selectionChanged() |
97 | { |
98 | if ( ui.listView->selectionModel()->hasSelection() ) { |
99 | setValid( true ); |
100 | } else { |
101 | setValid( false ); |
102 | } |
103 | } |
104 | |
105 | void TypePage::leavePageNext() |
106 | { |
107 | if ( !ui.listView->selectionModel()->hasSelection() ) |
108 | return; |
109 | const QModelIndex index = ui.listView->selectionModel()->selectedIndexes().first(); |
110 | Global::setAssistant( index.data( Qt::UserRole ).toString() ); |
111 | } |
112 | |
113 | QTreeView *TypePage::treeview() const |
114 | { |
115 | return ui.listView; |
116 | } |
117 | |
118 | |