summaryrefslogtreecommitdiffstats
path: root/kttsd/kcmkttsmgr/selectevent.cpp
blob: 5b33fc240fb411f449cd138738d5369ba19e4adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
  Dialog to allow user to select a KNotify application and event.
  -------------------
  Copyright:
  (C) 2005 by Gary Cramblitt <[email protected]>
  -------------------
  Original author: Gary Cramblitt <[email protected]>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ******************************************************************************/

// TQt includes.
#include <tqcombobox.h>

// KDE includes
#include <kstandarddirs.h>
#include <tdeconfig.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdelistview.h>
#include <kiconloader.h>

// KTTS includes
#include "utils.h"
#include "selectevent.h"

SelectEvent::SelectEvent(TQWidget* parent, const char* name, WFlags fl, const TQString& initEventSrc)
    : SelectEventWidget(parent,name,fl)
{
    // Load list of event sources (applications).
    TQStringList fullpaths =
        TDEGlobal::dirs()->findAllResources("data", "*/eventsrc", false, true );
    TQStringList::ConstIterator it = fullpaths.begin();
    TQStringList relativePaths;
    for ( ; it != fullpaths.end(); ++it)
    {
        TQString relativePath = *it;
        if ( relativePath.at(0) == '/' && TDEStandardDirs::exists( relativePath ) )
        {
            relativePath = makeRelative( relativePath );
            relativePaths.append(relativePath);
        }
    }
    relativePaths.sort();
    it = relativePaths.begin();
    for ( ; it != relativePaths.end(); ++it)
    {
        TQString relativePath = *it;
        if ( !relativePath.isEmpty() )
        {
            TDEConfig* config = new TDEConfig(relativePath, true, false, "data");
            config->setGroup( TQString::fromLatin1("!Global!") );
            TQString icon = config->readEntry(TQString::fromLatin1("IconName"),
                TQString::fromLatin1("misc"));
            TQString description = config->readEntry( TQString::fromLatin1("Comment"),
                i18n("No description available") );
            delete config;
            int index = relativePath.find( '/' );
            TQString appname;
            if ( index >= 0 )
                appname = relativePath.left( index );
            else
                kdDebug() << "Cannot determine application name from path: " << relativePath << endl;
            eventSrcComboBox->insertItem( SmallIcon( icon ), description );
            m_eventSrcNames.append( appname );
            if ( appname == initEventSrc ) KttsUtils::setCbItemFromText(eventSrcComboBox, description);
        }
    }
    slotEventSrcComboBox_activated(eventSrcComboBox->currentItem());
    connect (eventSrcComboBox, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotEventSrcComboBox_activated(int)));
}

SelectEvent::~SelectEvent() { }

void SelectEvent::slotEventSrcComboBox_activated(int index)
{
    eventsListView->clear();
    TQListViewItem* item = 0;
    TQString eventSrc = m_eventSrcNames[index];
    TQString configFilename = eventSrc + TQString::fromLatin1( "/eventsrc" );
    TDEConfig* config = new TDEConfig( configFilename, true, false, "data" );
    TQStringList eventNames = config->groupList();
    uint eventNamesCount = eventNames.count();
    for (uint ndx = 0; ndx < eventNamesCount; ++ndx)
    {
        TQString eventName = eventNames[ndx];
        if ( eventName != "!Global!" )
        {
            config->setGroup( eventName );
            TQString eventDesc = config->readEntry( TQString::fromLatin1( "Comment" ),
                config->readEntry( TQString::fromLatin1( "Name" )));
            if ( !item )
                item = new TDEListViewItem( eventsListView, eventDesc, eventName );
            else
                item = new TDEListViewItem( eventsListView, item, eventDesc, eventName );
        }
    }
    delete config;
    eventsListView->sort();
    item = eventsListView->lastChild();
    TQString eventDesc = i18n("All other %1 events").arg(eventSrcComboBox->currentText());
    if ( !item )
        item = new TDEListViewItem( eventsListView, eventDesc, "default" );
    else
        item = new TDEListViewItem( eventsListView, item, eventDesc, "default" );

}

TQString SelectEvent::getEventSrc()
{
    return m_eventSrcNames[eventSrcComboBox->currentItem()];
}

TQString SelectEvent::getEvent()
{
    TQListViewItem* item = eventsListView->currentItem();
    if ( item )
        return item->text(1);
    else
        return TQString();
}

// returns e.g. "twin/eventsrc" from a given path
// "/opt/trinity/share/apps/twin/eventsrc"
TQString SelectEvent::makeRelative( const TQString& fullPath )
{
    int slash = fullPath.findRev( '/' ) - 1;
    slash = fullPath.findRev( '/', slash );

    if ( slash < 0 )
        return TQString();

    return fullPath.mid( slash+1 );
}


#include "selectevent.moc"