~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to khotkeys/libkhotkeysprivate/actions/command_url_action.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>
 
3
   Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
 
4
 
 
5
   This library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public
 
7
   License version 2 as published by the Free Software Foundation.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "actions/actions.h"
 
21
#include "action_data/action_data.h"
 
22
 
 
23
#include <KDE/KAuthorized>
 
24
#include <KDE/KConfigGroup>
 
25
#include <KDE/KDebug>
 
26
#include <KDE/KRun>
 
27
#include <KDE/KService>
 
28
#include <KDE/KUriFilter>
 
29
#include <kworkspace/kworkspace.h>
 
30
 
 
31
 
 
32
namespace KHotKeys {
 
33
 
 
34
CommandUrlActionVisitor::~CommandUrlActionVisitor()
 
35
    {}
 
36
 
 
37
 
 
38
CommandUrlAction::CommandUrlAction( ActionData* data_P, const QString& command_url_P )
 
39
    : Action( data_P ), _command_url( command_url_P )
 
40
    {
 
41
    }
 
42
 
 
43
 
 
44
QString CommandUrlAction::command_url() const
 
45
    {
 
46
    return _command_url;
 
47
    }
 
48
 
 
49
 
 
50
void CommandUrlAction::accept(ActionVisitor& visitor)
 
51
    {
 
52
    if (CommandUrlActionVisitor *v = dynamic_cast<CommandUrlActionVisitor*>(&visitor))
 
53
        {
 
54
        v->visit(*this);
 
55
        }
 
56
    else
 
57
        {
 
58
        kDebug() << "Visitor error";
 
59
        }
 
60
    }
 
61
 
 
62
 
 
63
void CommandUrlAction::cfg_write( KConfigGroup& cfg_P ) const
 
64
    {
 
65
    base::cfg_write( cfg_P );
 
66
    cfg_P.writeEntry( "CommandURL", command_url());
 
67
    cfg_P.writeEntry( "Type", "COMMAND_URL" ); // overwrites value set in base::cfg_write()
 
68
    }
 
69
 
 
70
 
 
71
Action* CommandUrlAction::copy( ActionData* data_P ) const
 
72
    {
 
73
    return new CommandUrlAction( data_P, command_url());
 
74
    }
 
75
 
 
76
 
 
77
const QString CommandUrlAction::description() const
 
78
    {
 
79
    return i18n( "Command/URL : " ) + command_url();
 
80
    }
 
81
 
 
82
 
 
83
void CommandUrlAction::execute()
 
84
    {
 
85
    kDebug();
 
86
    if( command_url().isEmpty())
 
87
        return;
 
88
    KUriFilterData uri;
 
89
    QString cmd = command_url();
 
90
    static bool sm_ready = false;
 
91
    if( !sm_ready )
 
92
        {
 
93
        KWorkSpace::propagateSessionManager();
 
94
        sm_ready = true;
 
95
        }
 
96
//    int space_pos = command_url().find( ' ' );
 
97
//    if( command_url()[ 0 ] != '\'' && command_url()[ 0 ] != '"' && space_pos > -1
 
98
//        && command_url()[ space_pos - 1 ] != '\\' )
 
99
//        cmd = command_url().left( space_pos ); // get first 'word'
 
100
    uri.setData( cmd );
 
101
    KUriFilter::self()->filterUri( uri );
 
102
    if( uri.uri().isLocalFile() && !uri.uri().hasRef() )
 
103
        cmd = uri.uri().toLocalFile();
 
104
    else
 
105
        cmd = uri.uri().url();
 
106
    switch( uri.uriType())
 
107
        {
 
108
        case KUriFilterData::LocalFile:
 
109
        case KUriFilterData::LocalDir:
 
110
        case KUriFilterData::NetProtocol:
 
111
        case KUriFilterData::Help:
 
112
            {
 
113
            ( void ) new KRun( uri.uri(),0L);
 
114
          break;
 
115
            }
 
116
        case KUriFilterData::Executable:
 
117
            {
 
118
            if (!KAuthorized::authorizeKAction("shell_access"))
 
119
                return;
 
120
            if( !uri.hasArgsAndOptions())
 
121
                {
 
122
                KService::Ptr service = KService::serviceByDesktopName( cmd );
 
123
                if( service )
 
124
                    {
 
125
                    KRun::run( *service, KUrl::List(), NULL );
 
126
                  break;
 
127
                    }
 
128
                }
 
129
            // fall though
 
130
            }
 
131
        case KUriFilterData::Shell:
 
132
            {
 
133
            if (!KAuthorized::authorizeKAction("shell_access"))
 
134
                return;
 
135
            if( !KRun::runCommand(
 
136
                cmd + ( uri.hasArgsAndOptions() ? uri.argsAndOptions() : "" ),
 
137
                cmd, uri.iconName(), NULL )) {
 
138
                // CHECKME ?
 
139
             }
 
140
          break;
 
141
            }
 
142
        default: // error
 
143
          return;
 
144
        }
 
145
    }
 
146
 
 
147
 
 
148
void CommandUrlAction::set_command_url( const QString &command )
 
149
    {
 
150
    _command_url = command;
 
151
    }
 
152
 
 
153
} // namespace KHotKeys