summaryrefslogtreecommitdiffstats
path: root/konq-plugins/sidebar/metabar/src/serviceloader.cpp
blob: 06a913ad3329d4bd8a3c7f39651c6522e6cf5acf (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
/***************************************************************************
 *   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 "serviceloader.h"

#include <tqdir.h>
#include <tqvaluelist.h>
#include <tqptrlist.h>

#include <dcopclient.h>

#include <tdeapplication.h>
#include <kdebug.h>
#include <tdeactioncollection.h>
#include <kiconloader.h>
#include <tdeaction.h>
#include <tdeshortcut.h>
#include <ksimpleconfig.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>


ServiceLoader::ServiceLoader(TQWidget *parent, const char *name) : TQObject(parent, name)
{  
  popups.setAutoDelete(true);
}

ServiceLoader::~ServiceLoader()
{
}

void ServiceLoader::loadServices(const KFileItem item, DOM::DOMString &html, int &count)
{
  popups.clear();
  
  KURL url = item.url();
  TQString mimeType = item.mimetype();
  TQString mimeGroup = mimeType.left(mimeType.find('/'));
  
  urlList.clear();
  urlList.append(url);
  
  TQStringList dirs = TDEGlobal::dirs()->findDirs( "data", "konqueror/servicemenus/" );
  TDEConfig config("metabarrc", true, false);
  config.setGroup("General");
  int maxActions = config.readNumEntry("MaxActions");
  bool matchAll = false; // config.readBoolEntry("MatchAll");
  
  int id = 0;
  TQString idString;
  
  for(TQStringList::Iterator dit = dirs.begin(); dit != dirs.end(); ++dit){
    idString.setNum(id);
  
    TQDir dir(*dit);
    TQStringList entries = dir.entryList("*.desktop", TQDir::Files);
    
    for(TQStringList::Iterator eit = entries.begin(); eit != entries.end(); ++eit){
      KSimpleConfig cfg( *dit + *eit, true );
      cfg.setDesktopGroup();
      
      if(cfg.hasKey("X-TDE-ShowIfRunning" )){
        const TQString app = cfg.readEntry( "X-TDE-ShowIfRunning" );
        if(!kapp->dcopClient()->isApplicationRegistered(app.utf8())){
          continue;
        }
      }

      if(cfg.hasKey("X-TDE-Protocol")){
        const TQString protocol = cfg.readEntry( "X-TDE-Protocol" );
        if(protocol != url.protocol()){
          continue;
        }
      }
      
      else if(url.protocol() == "trash"){
        continue;
      }

      if(cfg.hasKey("X-TDE-Require")){
        const TQStringList capabilities = cfg.readListEntry( "X-TDE-Require" );
        if (capabilities.contains( "Write" )){
          continue;
        }
      }

      if ( cfg.hasKey( "Actions" ) && cfg.hasKey( "X-TDE-ServiceTypes" ) ){
          const TQStringList types = cfg.readListEntry( "X-TDE-ServiceTypes" );
          const TQStringList excludeTypes = cfg.readListEntry( "X-TDE-ExcludeServiceTypes" );
          bool ok = false;

          for (TQStringList::ConstIterator it = types.begin(); it != types.end() && !ok; ++it){
            bool checkTheMimetypes = false;
          
            if(matchAll){
            // first check if we have an all mimetype
              if (*it == "all/all" || *it == "allfiles"){
                checkTheMimetypes = true;
              }
  
              // next, do we match all files?
              if (!ok && !item.isDir() && *it == "all/allfiles"){
                checkTheMimetypes = true;
              }
            }

            // if we have a mimetype, see if we have an exact or a type globbed match
            if(!ok && (!mimeType.isEmpty() && *it == mimeType) ||
              (!mimeGroup.isEmpty() && ((*it).right(1) == "*" && (*it).left((*it).find('/')) == mimeGroup)))
            {
              checkTheMimetypes = true;
            }

            if(checkTheMimetypes){
              ok = true;
              
              for(TQStringList::ConstIterator itex = excludeTypes.begin(); itex != excludeTypes.end(); ++itex){
                if( ((*itex).right(1) == "*" && (*itex).left((*itex).find('/')) == mimeGroup) ||
                    ((*itex) == mimeType))
                {
                  ok = false;
                  break;
                }
              }
            }
          }
        if (ok){
          const TQString priority = cfg.readEntry("X-TDE-Priority");
          const TQString submenuName = cfg.readEntry( "X-TDE-Submenu" );
          bool usePopup = false;
          TDEPopupMenu *popup;
          
          if(!submenuName.isEmpty()){
            usePopup = true;
            
            if(popups[submenuName]){
              popup = popups[submenuName];
            }
            else{              
              MetabarWidget::addEntry(html, submenuName, "servicepopup://" + idString, "1rightarrow", "popup" + idString, count < maxActions ? TQString() : TQString("hiddenaction"), count >= maxActions);
              
              popup = new TDEPopupMenu();
              popups.insert(idString, popup);
              
              count++;
            }
          }
          
          TQValueList<KDEDesktopMimeType::Service> list = KDEDesktopMimeType::userDefinedServices( *dit + *eit, url.isLocalFile());
          
          for (TQValueList<KDEDesktopMimeType::Service>::iterator it = list.begin(); it != list.end(); ++it){
          
            if(usePopup){
              TDEAction *action = new TDEAction((*it).m_strName, (*it).m_strIcon, TDEShortcut(), TQT_TQOBJECT(this), TQT_SLOT(runAction()), TQT_TQOBJECT(popup), idString.utf8());
              action->plug(popup);
            }
            else{
              MetabarWidget::addEntry(html, (*it).m_strName, "service://" + idString, (*it).m_strIcon, TQString(), count < maxActions ? TQString() : TQString("hiddenaction"), count >= maxActions);
              count++;
            }
            
            services.insert(idString, *it);
            id++;
            idString.setNum(id);
          }
        }
      }
    }
  }
}

void ServiceLoader::runAction()
{    
  KDEDesktopMimeType::Service s = services[sender()->name()];
  if(!s.isEmpty()){
    KDEDesktopMimeType::executeService(urlList, s);
  }
}

void ServiceLoader::runAction(const TQString& name)
{
  KDEDesktopMimeType::Service s = services[name];
  if(!s.isEmpty()){
    KDEDesktopMimeType::executeService(urlList, s);
  }
}

void ServiceLoader::showPopup(const TQString &popup, const TQPoint &point)
{
  TDEPopupMenu *p = popups[popup];
  if(p){
    p->exec(point);
  }
}

#include "serviceloader.moc"