summaryrefslogtreecommitdiffstats
path: root/kioslave/media/libmediacommon/notifiersettings.cpp
blob: 332832b9861c91be71dfd4a259f3d2f8abfe578f (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* 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 "notifiersettings.h"

#include <kglobal.h>
#include <kdesktopfile.h>
#include <kstandarddirs.h>
#include <tqdir.h>
#include <tqfile.h>

#include "notifieropenaction.h"
#include "notifiernothingaction.h"


NotifierSettings::NotifierSettings()
{
	m_supportedMimetypes.append( "media/removable_unmounted" );
	m_supportedMimetypes.append( "media/removable_unmounted_encrypted" );
	m_supportedMimetypes.append( "media/removable_unmounted_decrypted" );
	m_supportedMimetypes.append( "media/removable_mounted" );
	m_supportedMimetypes.append( "media/removable_mounted_decrypted" );
	m_supportedMimetypes.append( "media/camera_unmounted" );
	m_supportedMimetypes.append( "media/camera_mounted" );
	m_supportedMimetypes.append( "media/gphoto2camera" );
	m_supportedMimetypes.append( "media/cdrom_unmounted" );
	m_supportedMimetypes.append( "media/cdrom_unmounted_encrypted" );
	m_supportedMimetypes.append( "media/cdrom_unmounted_decrypted" );
	m_supportedMimetypes.append( "media/cdrom_mounted" );
	m_supportedMimetypes.append( "media/cdrom_mounted_decrypted" );
	m_supportedMimetypes.append( "media/dvd_unmounted" );
	m_supportedMimetypes.append( "media/dvd_unmounted_encrypted" );
	m_supportedMimetypes.append( "media/dvd_unmounted_decrypted" );
	m_supportedMimetypes.append( "media/dvd_mounted" );
	m_supportedMimetypes.append( "media/dvd_mounted_decrypted" );
	m_supportedMimetypes.append( "media/cdwriter_unmounted" );
	m_supportedMimetypes.append( "media/cdwriter_unmounted_encrypted" );
	m_supportedMimetypes.append( "media/cdwriter_unmounted_decrypted" );
	m_supportedMimetypes.append( "media/cdwriter_mounted" );
	m_supportedMimetypes.append( "media/cdwriter_mounted_decrypted" );
	m_supportedMimetypes.append( "media/blankcd" );
	m_supportedMimetypes.append( "media/blankdvd" );
	m_supportedMimetypes.append( "media/audiocd" );
	m_supportedMimetypes.append( "media/dvdvideo" );
	m_supportedMimetypes.append( "media/vcd" );
	m_supportedMimetypes.append( "media/svcd" );
	
	reload();
}

NotifierSettings::~NotifierSettings()
{
	while ( !m_actions.isEmpty() )
	{
		NotifierAction *a = m_actions.first();
		m_actions.remove( a );
		delete a;
	}

	while ( !m_deletedActions.isEmpty() )
	{
		NotifierServiceAction *a = m_deletedActions.first();
		m_deletedActions.remove( a );
		delete a;
	}
}

TQValueList<NotifierAction*> NotifierSettings::actions()
{
	return m_actions;
}

const TQStringList &NotifierSettings::supportedMimetypes()
{
	return m_supportedMimetypes;
}

TQValueList<NotifierAction*> NotifierSettings::actionsForMimetype( const TQString &mimetype )
{
	TQValueList<NotifierAction*> result;

	TQValueList<NotifierAction*>::iterator it = m_actions.begin();
	TQValueList<NotifierAction*>::iterator end = m_actions.end();

	for ( ; it!=end; ++it )
	{
		if ( (*it)->supportsMimetype( mimetype ) )
		{
			result.append( *it );
		}
	}
	
	return result;
}

bool NotifierSettings::addAction( NotifierServiceAction *action )
{
	if ( !m_idMap.tqcontains( action->id() ) )
	{
		m_actions.insert( --m_actions.end(), action );
		m_idMap[ action->id() ] = action;
		return true;
	}
	return false;
}

bool NotifierSettings::deleteAction( NotifierServiceAction *action )
{
	if ( action->isWritable() )
	{
		m_actions.remove( action );
		m_idMap.remove( action->id() );
		m_deletedActions.append( action );

		TQStringList auto_mimetypes = action->autoMimetypes();
		TQStringList::iterator it = auto_mimetypes.begin();
		TQStringList::iterator end = auto_mimetypes.end();

		for ( ; it!=end; ++it )
		{
			action->removeAutoMimetype( *it );
			m_autoMimetypesMap.remove( *it );
		}
		
		return true;
	}
	return false;
}

void NotifierSettings::setAutoAction( const TQString &mimetype, NotifierAction *action )
{
	resetAutoAction( mimetype );
	m_autoMimetypesMap[mimetype] = action;
	action->addAutoMimetype( mimetype );
}


void NotifierSettings::resetAutoAction( const TQString &mimetype )
{
	if ( m_autoMimetypesMap.tqcontains( mimetype ) )
	{
		NotifierAction *action = m_autoMimetypesMap[mimetype];
		action->removeAutoMimetype( mimetype );
		m_autoMimetypesMap.remove(mimetype);
	}
}

void NotifierSettings::clearAutoActions()
{
	TQMap<TQString,NotifierAction*>::iterator it = m_autoMimetypesMap.begin();
	TQMap<TQString,NotifierAction*>::iterator end = m_autoMimetypesMap.end();

	for ( ; it!=end; ++it )
	{
		NotifierAction *action = it.data();
		TQString mimetype = it.key();

		if ( action )
			action->removeAutoMimetype( mimetype );
		m_autoMimetypesMap[mimetype] = 0L;
	}
}

NotifierAction *NotifierSettings::autoActionForMimetype( const TQString &mimetype )
{
	if ( m_autoMimetypesMap.tqcontains( mimetype ) )
	{
		return m_autoMimetypesMap[mimetype];
	}
	else
	{
		return 0L;
	}
}

void NotifierSettings::reload()
{
	while ( !m_actions.isEmpty() )
	{
		NotifierAction *a = m_actions.first();
		m_actions.remove( a );
		delete a;
	}

	while ( !m_deletedActions.isEmpty() )
	{
		NotifierServiceAction *a = m_deletedActions.first();
		m_deletedActions.remove( a );
		delete a;
	}

	m_idMap.clear();
	m_autoMimetypesMap.clear();
	
	NotifierOpenAction *open = new NotifierOpenAction();
	m_actions.append( open );
	m_idMap[ open->id() ] = open;

	TQValueList<NotifierServiceAction*> services = listServices();

	TQValueList<NotifierServiceAction*>::iterator serv_it = services.begin();
	TQValueList<NotifierServiceAction*>::iterator serv_end = services.end();
	
	for ( ; serv_it!=serv_end; ++serv_it )
	{
		m_actions.append( *serv_it );
		m_idMap[ (*serv_it)->id() ] = *serv_it;
	}
	
	NotifierNothingAction *nothing = new NotifierNothingAction();
	m_actions.append( nothing );
	m_idMap[ nothing->id() ] = nothing;

	KConfig config( "medianotifierrc", true );
	TQMap<TQString,TQString> auto_actions_map = config.entryMap( "Auto Actions" );

	TQMap<TQString,TQString>::iterator auto_it = auto_actions_map.begin();
	TQMap<TQString,TQString>::iterator auto_end = auto_actions_map.end();
	
	for ( ; auto_it!=auto_end; ++auto_it )
	{
		TQString mime = auto_it.key();
		TQString action_id = auto_it.data();

		if ( m_idMap.tqcontains( action_id ) )
		{
			setAutoAction( mime, m_idMap[action_id] );
		}
		else
		{
			config.deleteEntry( mime );
		}
	}
}
void NotifierSettings::save()
{
	TQValueList<NotifierAction*>::iterator act_it = m_actions.begin();
	TQValueList<NotifierAction*>::iterator act_end = m_actions.end();

	for ( ; act_it!=act_end; ++act_it )
	{
		NotifierServiceAction *service;
		if ( ( service=dynamic_cast<NotifierServiceAction*>( *act_it ) )
		  && service->isWritable() )
		{
			service->save();
		}
	}
	
	while ( !m_deletedActions.isEmpty() )
	{
		NotifierServiceAction *a = m_deletedActions.first();
		m_deletedActions.remove( a );
		TQFile::remove( a->filePath() );
		delete a;
	}
	
	KSimpleConfig config( "medianotifierrc" );
	config.setGroup( "Auto Actions" );
	
	TQMap<TQString,NotifierAction*>::iterator auto_it = m_autoMimetypesMap.begin();
	TQMap<TQString,NotifierAction*>::iterator auto_end = m_autoMimetypesMap.end();

	for ( ; auto_it!=auto_end; ++auto_it )
	{
		if ( auto_it.data()!=0L )
		{
			config.writeEntry( auto_it.key(), auto_it.data()->id() );
		}
		else
		{
			config.deleteEntry( auto_it.key() );
		}
	}
}

TQValueList<NotifierServiceAction*> NotifierSettings::loadActions( KDesktopFile &desktop ) const
{
	desktop.setDesktopGroup();

	TQValueList<NotifierServiceAction*> services;
	
	const TQString filename = desktop.fileName();
	const TQStringList mimetypes = desktop.readListEntry( "ServiceTypes" );

	TQValueList<KDEDesktopMimeType::Service> type_services
		= KDEDesktopMimeType::userDefinedServices(filename, true);

	TQValueList<KDEDesktopMimeType::Service>::iterator service_it = type_services.begin();
	TQValueList<KDEDesktopMimeType::Service>::iterator service_end = type_services.end();
	for (; service_it!=service_end; ++service_it)
	{
		NotifierServiceAction *service_action
			= new NotifierServiceAction();
		
		service_action->setService( *service_it );
		service_action->setFilePath( filename );
		service_action->setMimetypes( mimetypes );
		
		services += service_action;
	}

	return services;
}


bool NotifierSettings::shouldLoadActions( KDesktopFile &desktop, const TQString &mimetype ) const
{
	desktop.setDesktopGroup();

	if ( desktop.hasKey( "Actions" )
	  && desktop.hasKey( "ServiceTypes" )
	  && !desktop.readBoolEntry( "X-KDE-MediaNotifierHide", false )  )
	{
		const TQStringList actions = desktop.readListEntry( "Actions" );

		if ( actions.size()!=1 )
		{
			return false;
		}
		
		const TQStringList types = desktop.readListEntry( "ServiceTypes" );

		if ( mimetype.isEmpty() )
		{
			TQStringList::ConstIterator type_it = types.begin();
			TQStringList::ConstIterator type_end = types.end();
			for (; type_it != type_end; ++type_it)
			{
				if ( (*type_it).startsWith( "media/" ) )
				{
					return true;
				}
			}
		}
		else if ( types.tqcontains(mimetype) )
		{
			return true;
		}
	}

	return false;
}

TQValueList<NotifierServiceAction*> NotifierSettings::listServices( const TQString &mimetype ) const
{
	TQValueList<NotifierServiceAction*> services;
	TQStringList dirs = KGlobal::dirs()->findDirs("data", "konqueror/servicemenus/");
	
	TQStringList::ConstIterator dir_it = dirs.begin();
	TQStringList::ConstIterator dir_end = dirs.end();
	for (; dir_it != dir_end; ++dir_it)
	{
		TQDir dir( *dir_it );

		TQStringList entries = dir.entryList( "*.desktop", TQDir::Files );

		TQStringList::ConstIterator entry_it = entries.begin();
		TQStringList::ConstIterator entry_end = entries.end();

		for (; entry_it != entry_end; ++entry_it )
		{
			TQString filename = *dir_it + *entry_it;
			
			KDesktopFile desktop( filename, true );
			
			if ( shouldLoadActions(desktop, mimetype) )
			{
				services+=loadActions(desktop);
			}
		}
	}

	return services;
}