summaryrefslogtreecommitdiffstats
path: root/konq-plugins/sidebar/metabar/src/settingsplugin.cpp
blob: 91bb96d44e6ed4144cabcd0ab93bbd1d9a7685ce (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
/***************************************************************************
 *   Copyright (C) 2005 by Florian Roth   *
 *   florian@synatic.net   *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/
 
#include "metabarwidget.h"
#include "settingsplugin.h"

#include <kcmoduleinfo.h>
#include <kservice.h>
#include <klocale.h>

#include <html_document.h>
#include <html_element.h>
#include <dom_string.h>

SettingsPlugin::SettingsPlugin(KHTMLPart* html, MetabarFunctions *functions, const char *name) : ProtocolPlugin (html, functions, name)
{
  list_job = 0;
}

SettingsPlugin::~SettingsPlugin()
{
}

void SettingsPlugin::killJobs()
{  
  if(list_job){
    list_job->kill();
    list_job = 0;
  }
}

void SettingsPlugin::deactivate()
{   
  m_functions->hide("actions");
  m_functions->hide("info");
}

void SettingsPlugin::loadActions(DOM::HTMLElement node)
{
  KURL url = m_items.getFirst()->url();
  
  if(url.path().endsWith("/")){
    list_job = KIO::listDir(url, true, false);
    connect(list_job, SIGNAL(entries(KIO::Job *, const KIO::UDSEntryList &)), this, SLOT(slotGotEntries(KIO::Job *, const KIO::UDSEntryList &)));
    connect(list_job, SIGNAL(result(KIO::Job *)), this, SLOT(slotJobFinished(KIO::Job *)));
  
    m_functions->show("actions");
  }
  else{
    QString path = url.path();
    QString name = url.filename();
    
    KService::Ptr service = KService::serviceByStorageId(name);
    if(service && service->isValid()){
      KCModuleInfo kcminfo(service);
      
      DOM::DOMString innerHTML;      
      MetabarWidget::addEntry(innerHTML, i18n("Run"), "kcmshell:/" + name, kcminfo.icon());
      node.setInnerHTML(innerHTML);
      
      m_functions->show("actions");
    }
    else{
      m_functions->hide("actions");
    }
  }
}

void SettingsPlugin::loadInformation(DOM::HTMLElement node)
{
  KURL url = m_items.getFirst()->url();
  if(url.path().endsWith("/")){
    m_functions->hide("info");
  }
  else{
    QString path = url.path();
    QString name = url.filename();
    
    KService::Ptr service = KService::serviceByStorageId(name);
    if(service && service->isValid()){
      KCModuleInfo kcminfo(service);
      
      bool needsRoot = kcminfo.needsRootPrivileges();
    
      DOM::DOMString innerHTML;
      innerHTML += "<ul class=\"info\"><b>" + i18n("Name") + ": </b>";
      innerHTML += kcminfo.moduleName();
      innerHTML += "</ul><ul class=\"info\"><b>" + i18n("Comment") + ": </b>";
      innerHTML += kcminfo.comment();
      innerHTML += "</ul>";
      
      if(needsRoot){
        innerHTML += "<ul class=\"info\"><b>";
        innerHTML += i18n("Needs root privileges");
        innerHTML += "</b></ul>";
      }
      node.setInnerHTML(innerHTML);
      
      m_functions->show("info");
    }
    else{
      m_functions->hide("info");
    }
  }
}

void SettingsPlugin::loadApplications(DOM::HTMLElement node)
{
  m_functions->hide("open");
}

void SettingsPlugin::loadPreview(DOM::HTMLElement node)
{
  m_functions->hide("preview");
}

void SettingsPlugin::loadBookmarks(DOM::HTMLElement node)
{
  m_functions->hide("bookmarks");
}

bool SettingsPlugin::handleRequest(const KURL &)
{  
  return false;
}

void SettingsPlugin::slotGotEntries(KIO::Job *job, const KIO::UDSEntryList &list)
{
  if(!job){
    return;
  }
  
  DOM::HTMLDocument doc = m_html->htmlDocument();
  DOM::HTMLElement node = doc.getElementById("actions");
  DOM::DOMString innerHTML;
  
  KIO::UDSEntryList::ConstIterator it     = list.begin();
  KIO::UDSEntryList::ConstIterator it_end = list.end();
  for(; it != it_end; ++it){
    QString name;
    QString icon;
    QString url;
    long type;
  
    KIO::UDSEntry::ConstIterator atomit     = (*it).begin();
    KIO::UDSEntry::ConstIterator atomit_end = (*it).end();
    for(; atomit != atomit_end; ++atomit){
      if((*atomit).m_uds == KIO::UDS_NAME){
        name = (*atomit).m_str;
      }
      else if((*atomit).m_uds == KIO::UDS_ICON_NAME){
        icon = (*atomit).m_str;
      }
      else if((*atomit).m_uds == KIO::UDS_URL){
        url = (*atomit).m_str;
      }
      
      else if((*atomit).m_uds == KIO::UDS_FILE_TYPE){
        type = (*atomit).m_long;
      }
    }
    
    kdDebug() << url << endl;
    
    if(type == S_IFREG){
      url = "kcmshell:/" + KURL(url).filename();
    }

    MetabarWidget::addEntry(innerHTML, name, url, icon);
  }
  
  node.setInnerHTML(innerHTML);
}

void SettingsPlugin::slotJobFinished(KIO::Job *job)
{
  if(list_job && job == list_job){
    list_job = 0;

    m_functions->adjustSize("actions");
  }
}

#include "settingsplugin.moc"