1 /***************************************************** vim:set ts=4 sw=4 sts=4:
2 Dialog to allow user to select a KNotify application and event.
5 (C) 2005 by Gary Cramblitt <garycramblitt@comcast.net>
7 Original author: Gary Cramblitt <garycramblitt@comcast.net>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 ******************************************************************************/
25 #include <qcombobox.h>
28 #include <kstandarddirs.h>
32 #include <klistview.h>
33 #include <kiconloader.h>
37 #include "selectevent.h"
39 SelectEvent::SelectEvent(QWidget
* parent
, const char* name
, WFlags fl
, const QString
& initEventSrc
)
40 : SelectEventWidget(parent
,name
,fl
)
42 // Load list of event sources (applications).
43 QStringList fullpaths
=
44 KGlobal::dirs()->findAllResources("data", "*/eventsrc", false, true );
45 QStringList::ConstIterator it
= fullpaths
.begin();
46 QStringList relativePaths
;
47 for ( ; it
!= fullpaths
.end(); ++it
)
49 QString relativePath
= *it
;
50 if ( relativePath
.at(0) == '/' && KStandardDirs::exists( relativePath
) )
52 relativePath
= makeRelative( relativePath
);
53 relativePaths
.append(relativePath
);
57 it
= relativePaths
.begin();
58 for ( ; it
!= relativePaths
.end(); ++it
)
60 QString relativePath
= *it
;
61 if ( !relativePath
.isEmpty() )
63 KConfig
* config
= new KConfig(relativePath
, true, false, "data");
64 config
->setGroup( QString::fromLatin1("!Global!") );
65 QString icon
= config
->readEntry(QString::fromLatin1("IconName"),
66 QString::fromLatin1("misc"));
67 QString description
= config
->readEntry( QString::fromLatin1("Comment"),
68 i18n("No description available") );
70 int index
= relativePath
.find( '/' );
73 appname
= relativePath
.left( index
);
75 kdDebug() << "Cannot determine application name from path: " << relativePath
<< endl
;
76 eventSrcComboBox
->insertItem( SmallIcon( icon
), description
);
77 m_eventSrcNames
.append( appname
);
78 if ( appname
== initEventSrc
) KttsUtils::setCbItemFromText(eventSrcComboBox
, description
);
81 slotEventSrcComboBox_activated(eventSrcComboBox
->currentItem());
82 connect (eventSrcComboBox
, SIGNAL(activated(int)), this, SLOT(slotEventSrcComboBox_activated(int)));
85 SelectEvent::~SelectEvent() { }
87 void SelectEvent::slotEventSrcComboBox_activated(int index
)
89 eventsListView
->clear();
90 QListViewItem
* item
= 0;
91 QString eventSrc
= m_eventSrcNames
[index
];
92 QString configFilename
= eventSrc
+ QString::fromLatin1( "/eventsrc" );
93 KConfig
* config
= new KConfig( configFilename
, true, false, "data" );
94 QStringList eventNames
= config
->groupList();
95 uint eventNamesCount
= eventNames
.count();
96 for (uint ndx
= 0; ndx
< eventNamesCount
; ++ndx
)
98 QString eventName
= eventNames
[ndx
];
99 if ( eventName
!= "!Global!" )
101 config
->setGroup( eventName
);
102 QString eventDesc
= config
->readEntry( QString::fromLatin1( "Comment" ),
103 config
->readEntry( QString::fromLatin1( "Name" )));
105 item
= new KListViewItem( eventsListView
, eventDesc
, eventName
);
107 item
= new KListViewItem( eventsListView
, item
, eventDesc
, eventName
);
111 eventsListView
->sort();
112 item
= eventsListView
->lastChild();
113 QString eventDesc
= i18n("All other %1 events").arg(eventSrcComboBox
->currentText());
115 item
= new KListViewItem( eventsListView
, eventDesc
, "default" );
117 item
= new KListViewItem( eventsListView
, item
, eventDesc
, "default" );
121 QString
SelectEvent::getEventSrc()
123 return m_eventSrcNames
[eventSrcComboBox
->currentItem()];
126 QString
SelectEvent::getEvent()
128 QListViewItem
* item
= eventsListView
->currentItem();
130 return item
->text(1);
132 return QString::null
;
135 // returns e.g. "kwin/eventsrc" from a given path
136 // "/opt/kde3/share/apps/kwin/eventsrc"
137 QString
SelectEvent::makeRelative( const QString
& fullPath
)
139 int slash
= fullPath
.findRev( '/' ) - 1;
140 slash
= fullPath
.findRev( '/', slash
);
143 return QString::null
;
145 return fullPath
.mid( slash
+1 );
149 #include "selectevent.moc"