summaryrefslogtreecommitdiffstats
path: root/src/k3bservicemenuinstaller.cpp
blob: 43413d97e0464d50388efd4fa190f0e8fb9f5c2a (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
/* 
 *
 * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
 * Copyright (C) 2006 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * 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.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bservicemenuinstaller.h"

#include <kglobal.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kmessagebox.h>
#include <kio/netaccess.h>
#include <kdebug.h>


class K3bServiceInstaller::Private
{
public:
  TQString app;
  TQStringList allServiceMenus;
  TQStringList allServiceMenuFiles;
  TQString konqiServicemenusFolder;

  void update() {
    // the list of installable servicemenus the application provides
    allServiceMenus = KGlobal::dirs()->findAllResources( "data",
							 "k3b/servicemenus/*.desktop",
							 false,
							 true );

    // just the filenames
    allServiceMenuFiles.clear();
    for( unsigned int i = 0; i < allServiceMenus.count(); ++i )
      allServiceMenuFiles.append( allServiceMenus[i].section( '/', -1 ) );

    // the local konqueror servicemenu folder (we just create it here to be on the safe side)
    konqiServicemenusFolder = KGlobal::dirs()->saveLocation( "data", "konqueror/servicemenus/", true );
  }
};


K3bServiceInstaller::K3bServiceInstaller( const TQString& appname )
{
  d = new Private;
  d->app = appname;
}


K3bServiceInstaller::~K3bServiceInstaller()
{
  delete d;
}


bool K3bServiceInstaller::allInstalled() const
{
  d->update();

  for( unsigned int i = 0; i < d->allServiceMenuFiles.count(); ++i )
    if( !KIO::NetAccess::exists( d->konqiServicemenusFolder + d->allServiceMenuFiles[i], true, 0 ) ) {
      kdDebug() << "(K3bServiceInstaller) service menu " << d->konqiServicemenusFolder << d->allServiceMenuFiles[i]
		<< " does not exist." << endl;
      return false;
    }
  
  return true;
}


bool K3bServiceInstaller::install( TQWidget* parent )
{
  d->update();

  bool success = true;

  // simply link all the globally installed K3b service menus to the local konqi service menu folder
  for( unsigned int i = 0; i < d->allServiceMenus.count(); ++i )
    if( !KIO::NetAccess::file_copy( KURL::fromPathOrURL( d->allServiceMenus[i] ), 
				    KURL::fromPathOrURL( d->konqiServicemenusFolder + d->allServiceMenuFiles[i] ), -1, 
				    true, false, parent ) )
      success = false;

  if( !success && parent )
    KMessageBox::error( parent,
			KIO::NetAccess::lastErrorString(),
			i18n("Failed to copy service menu files") );

  return success;
}


bool K3bServiceInstaller::remove( TQWidget* parent )
{
  d->update();

  bool success = true;

  for( unsigned int i = 0; i < d->allServiceMenuFiles.count(); ++i )
    if( KIO::NetAccess::exists( d->konqiServicemenusFolder + d->allServiceMenuFiles[i], true, parent ) )
      if( !KIO::NetAccess::del( d->konqiServicemenusFolder + d->allServiceMenuFiles[i], parent ) )
	success = false;

  if( !success && parent )
    KMessageBox::error( parent,
			KIO::NetAccess::lastErrorString(),
			i18n("Failed to remove service menu files") );

  return success;
}