summaryrefslogtreecommitdiffstats
path: root/kioslave/media/kcmodule/serviceconfigdialog.cpp
blob: 1a127d12beb0e8ed8e4a9747094dd69d26d29c9b (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
/* 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 "serviceconfigdialog.h"

#include <klocale.h>
#include <klineedit.h>
#include <kactionselector.h>
#include <kicondialog.h>
#include <qlistbox.h>
#include <kservice.h>
#include <kopenwith.h>
#include <kpushbutton.h>
#include <kiconloader.h>
#include <qpixmap.h>
#include <qiconset.h>


#include "mimetypelistboxitem.h"

ServiceConfigDialog::ServiceConfigDialog(NotifierServiceAction *action,
                                         const QStringList &mimetypesList,
                                         QWidget* parent, const char* name)
	: KDialogBase(parent, name, true, i18n("Edit Service"), Ok|Cancel, Ok, true),
	  m_action(action)
{
	m_view = new ServiceView(this);
	
	m_view->iconButton->setIcon( m_action->iconName() );
	m_view->labelEdit->setText( m_action->label() );
	m_view->commandEdit->setText( m_action->service().m_strExec );

	QIconSet iconSet = SmallIconSet("configure");
	QPixmap pixMap = iconSet.pixmap( QIconSet::Small, QIconSet::Normal );
	m_view->commandButton->setIconSet( iconSet );
	m_view->commandButton->setFixedSize( pixMap.width()+8, pixMap.height()+8 );
	
	m_iconChanged = false;

	QStringList all_mimetypes = mimetypesList;
	QStringList action_mimetypes = action->mimetypes();

	QStringList::iterator it = all_mimetypes.begin();
	QStringList::iterator end = all_mimetypes.end();

	for (  ; it!=end; ++it )
	{
		QListBox *list;
		
		if ( action_mimetypes.contains( *it ) )
		{
			list = m_view->mimetypesSelector->selectedListBox();
		}
		else
		{
			list = m_view->mimetypesSelector->availableListBox();
		}
		
		new MimetypeListBoxItem(  *it, list );
	}
	
	setMainWidget(m_view);
	setCaption( m_action->label() );

	connect( m_view->iconButton, SIGNAL( iconChanged(QString) ),
	         this, SLOT( slotIconChanged() ) );
	connect( m_view->commandButton, SIGNAL( clicked() ),
	         this, SLOT( slotCommand() ) );
}

bool operator==( KDEDesktopMimeType::Service s1, KDEDesktopMimeType::Service s2 )
{
	return ( s1.m_strName==s2.m_strName )
	    && ( s1.m_strIcon==s2.m_strIcon )
	    && ( s1.m_strExec==s2.m_strExec );
}

bool operator!=( KDEDesktopMimeType::Service s1, KDEDesktopMimeType::Service s2 )
{
	return !( s1==s2 );
}

void ServiceConfigDialog::slotOk()
{
	KDEDesktopMimeType::Service service;
	service.m_strName = m_view->labelEdit->text();
	service.m_strIcon = m_view->iconButton->icon();
	service.m_strExec = m_view->commandEdit->text();

	QStringList mimetypes;
	
	uint list_count = m_view->mimetypesSelector->selectedListBox()->count();
	for( uint i=0; i < list_count; ++i )
	{
		QListBoxItem *item = m_view->mimetypesSelector->selectedListBox()->item(i);
		MimetypeListBoxItem *mime_item = static_cast<MimetypeListBoxItem*>( item );
		mimetypes.append( mime_item->mimetype() );
	}

	if ( service!=m_action->service() || mimetypes!=m_action->mimetypes() )
	{
		m_action->setService( service );
		m_action->setMimetypes( mimetypes );
		accept();
	}
	else
	{
		reject();
	}
}

void ServiceConfigDialog::slotIconChanged()
{
	m_iconChanged = true;
}

void ServiceConfigDialog::slotCommand()
{
	KOpenWithDlg d(this);
	int value = d.exec();
	if ( value == QDialog::Accepted )
	{
		KService::Ptr service = d.service();
		if ( service != 0L )
		{			
			m_view->commandEdit->setText( service->exec() );
			if ( m_iconChanged == false )
			{
				m_view->iconButton->setIcon( service->icon() );
			}
		}
	}
}

#include "serviceconfigdialog.moc"