summaryrefslogtreecommitdiffstats
path: root/kioslave/media/kcmodule/notifiermodule.cpp
blob: 5c4e4c3573ecba7912eaf455253838d744e6b702 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* This file is part of the KDE Project
   Copyright (c) 2005 Jean-Remy Falleri <jr.falleri@laposte.net>
   Copyright (c) 2005 Kévin Ottens <ervin ipsquad net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

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

#include "notifiermodule.h"

#include <klocale.h>

#include <tqlayout.h>
#include <kcombobox.h>
#include <kpushbutton.h>
#include <kstdguiitem.h>

#include "notifiersettings.h"
#include "serviceconfigdialog.h"
#include "actionlistboxitem.h"
#include "mimetypelistboxitem.h"

NotifierModule::NotifierModule(TQWidget *parent, const char *name)
	: KCModule(parent, name)
{
	TQBoxLayout *layout = new TQVBoxLayout( this, 0, KDialog::spacingHint() );
	
	m_view = new NotifierModuleView( this );
	layout->addWidget( m_view );
	
	m_view->addButton->setGuiItem( KStdGuiItem::add() );
	m_view->editButton->setGuiItem( KStdGuiItem::properties() );
	m_view->deleteButton->setGuiItem( KStdGuiItem::del() );
	
	m_view->mimetypesCombo->insertItem( i18n("All Mime Types") );

	TQStringList mimetypes = m_settings.supportedMimetypes();

	TQStringList::iterator it = mimetypes.begin();
	TQStringList::iterator end = mimetypes.end();

	for ( ; it!=end; ++it )
	{
		new MimetypeListBoxItem( *it, m_view->mimetypesCombo->listBox() );
	}
	
	updateListBox();

	connect( m_view->mimetypesCombo, TQT_SIGNAL( activated(int) ),
	         this, TQT_SLOT( slotMimeTypeChanged(int) ) );
	connect( m_view->actionsList, TQT_SIGNAL( selectionChanged(TQListBoxItem*) ),
	         this, TQT_SLOT( slotActionSelected(TQListBoxItem*) ) );
	connect( m_view->addButton, TQT_SIGNAL( clicked() ),
	         this, TQT_SLOT( slotAdd() ) );
	connect( m_view->editButton, TQT_SIGNAL( clicked() ),
	         this, TQT_SLOT( slotEdit() ) );
	connect( m_view->deleteButton, TQT_SIGNAL( clicked() ),
	         this, TQT_SLOT( slotDelete() ) );
	connect( m_view->toggleAutoButton, TQT_SIGNAL( clicked() ),
	         this, TQT_SLOT( slotToggleAuto() ) );
}

NotifierModule::~NotifierModule()
{
}

void NotifierModule::load()
{
	m_settings.reload();
	slotMimeTypeChanged( m_view->mimetypesCombo->currentItem() );
}

void NotifierModule::save()
{
	m_settings.save();
}

void NotifierModule::defaults()
{
	m_settings.clearAutoActions();
	slotMimeTypeChanged( m_view->mimetypesCombo->currentItem() );
}

void NotifierModule::updateListBox()
{
	m_view->actionsList->clear();
	slotActionSelected( 0L );

	TQValueList<NotifierAction*> services;
	if ( m_mimetype.isEmpty() )
	{
		services = m_settings.actions();
	}
	else
	{
		services = m_settings.actionsForMimetype( m_mimetype );
	}

	TQValueList<NotifierAction*>::iterator it;
	
	for ( it = services.begin(); it != services.end(); ++it )
	{
		new ActionListBoxItem( *it, m_mimetype, m_view->actionsList );
	}
}

void NotifierModule::slotActionSelected(TQListBoxItem *item)
{
	NotifierAction *action = 0L;

	if ( item!=0L )
	{
		ActionListBoxItem *action_item
			= static_cast<ActionListBoxItem*>(item);
		action = action_item->action();
	}

	bool isWritable = action!=0L && action->isWritable();
	m_view->deleteButton->setEnabled( isWritable );
	m_view->editButton->setEnabled( isWritable );
	m_view->addButton->setEnabled( TRUE );
	m_view->toggleAutoButton->setEnabled( action!=0L && !m_mimetype.isEmpty() );
}

void NotifierModule::slotMimeTypeChanged(int index)
{
	if ( index == 0 )
	{
		m_mimetype = TQString();
	}
	else
	{
		TQListBoxItem *item = m_view->mimetypesCombo->listBox()->item( index );
		MimetypeListBoxItem *mime_item
			= static_cast<MimetypeListBoxItem*>( item );
		m_mimetype = mime_item->mimetype();
	}

	updateListBox();
}

void NotifierModule::slotAdd()
{
	NotifierServiceAction *action = new NotifierServiceAction();
	ServiceConfigDialog dialog(action, m_settings.supportedMimetypes(), this);
	
	int value = dialog.exec();
	
	if ( value == TQDialog::Accepted )
	{
		m_settings.tqaddAction( action );
		updateListBox();
		emit changed( true );
	}
	else
	{
		delete action;
	}
}

void NotifierModule::slotEdit()
{
	ActionListBoxItem *action_item
		= static_cast<ActionListBoxItem*>(m_view->actionsList->selectedItem());
	
	NotifierServiceAction * action = dynamic_cast<NotifierServiceAction*>( action_item->action() );
	if ( action )
	{
		ServiceConfigDialog dialog(action, m_settings.supportedMimetypes(), this);
		
		int value = dialog.exec();
		
		if ( value == TQDialog::Accepted )
		{
			updateListBox();
			emit changed( true );
		}
	}
}

void NotifierModule::slotDelete()
{
	ActionListBoxItem *action_item
		= static_cast<ActionListBoxItem*>(m_view->actionsList->selectedItem());
	
	NotifierServiceAction *action;
        action = dynamic_cast<NotifierServiceAction*>( action_item->action() );
	if ( action )
	{
		m_settings.deleteAction( action );
		updateListBox();
		emit changed( true );
	}
}

void NotifierModule::slotToggleAuto()
{
	ActionListBoxItem *action_item
		= static_cast<ActionListBoxItem*>( m_view->actionsList->selectedItem() );
	NotifierAction *action = action_item->action();

	int index = m_view->actionsList->index( action_item );
	
	if ( action->autoMimetypes().tqcontains( m_mimetype ) )
	{
		m_settings.resetAutoAction( m_mimetype );
	}
	else
	{
		m_settings.setAutoAction( m_mimetype, action );
	}

	updateListBox();
	emit changed( true );

	m_view->actionsList->setSelected( index, true );
}

#include "notifiermodule.moc"