summaryrefslogtreecommitdiffstats
path: root/kiostdetool/menueditComponent.cpp
blob: c087e911c3de2d2ebf1973f18ea550c156e17eb6 (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
/*
 *   menueditComponent.cpp
 *
 *   Copyright (C) 2004 Waldo Bastian <bastian@kde.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2 as
 *   published by the Free Software Foundation.
 *
 *   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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "menueditComponent.h"

#include <tqdir.h>
#include <tqdom.h>
#include <tqfileinfo.h>

#include <kapplication.h>
#include <kdebug.h>
#include <kmimetype.h>
#include <kprocess.h>
#include <ksavefile.h>
#include <ksimpleconfig.h>
#include <kstandarddirs.h>
#include <kurl.h>

#include "kioskrun.h"
#include "kiosksync.h"

MenuEditComponent::MenuEditComponent( TQObject *parent)
 : Component(parent)
{
}

MenuEditComponent::~MenuEditComponent()
{
}

void
MenuEditComponent::slotSetupPrepare()
{
   (void) KioskRun::self()->locateLocal("xdgconf-menu", "applications-kmenuedit.menu"); // Create dir
}

void
MenuEditComponent::slotSetupStarted()
{
}

static TQDomDocument loadDoc(const TQString &fileName)
{
   TQDomDocument doc;

   TQFile file( fileName );
   if ( !file.open( IO_ReadOnly ) )
   {
      kdWarning() << "Could not open " << fileName << endl;
      return doc;
   }
   TQString errorMsg;
   int errorRow;
   int errorCol;
   if ( !doc.setContent( &file, &errorMsg, &errorRow, &errorCol ) ) {
      kdWarning() << "Parse error in " << fileName << ", line " << errorRow << ", col " << errorCol << ": " << errorMsg << endl;
      file.close();
      return doc;
   }
   file.close();
   return doc;
}

static bool saveDoc(const TQString &fileName, TQDomDocument doc)
{
   KSaveFile saveFile(fileName);

   TQTextStream *stream = saveFile.textStream();
   if (!stream)
   {
      kdWarning() << "Could not write " << fileName << endl;
      return false;
   }
   (*stream) << doc.toString();

   if (!saveFile.close())
   {
      kdWarning() << "Could not write " << fileName << endl;
      return false;
   }

   return true;
}


bool
MenuEditComponent::setupFinished()
{
   bool result;
   TQString menuEditFile = KioskRun::self()->locateLocal("xdgconf-menu", "applications-kmenuedit.menu");
   TQString menuFile = KioskRun::self()->locate("xdgconf-menu", "applications.menu");
   TQString menuFileSave = KioskRun::self()->locateSave("xdgconf-menu", "applications.menu");

   kdDebug() << "MenuEditComponent: menuEditFile = " << menuEditFile << endl;
   kdDebug() << "MenuEditComponent: menuFile = " << menuFile << endl;
   kdDebug() << "MenuEditComponent: menuFileSave = " << menuFileSave << endl;

   TQDomDocument docChanges = loadDoc(menuEditFile);
   if (docChanges.isNull())
   {
       kdDebug() << "No menu changes." << endl;
       return true;
   }

   TQDomDocument doc = loadDoc(menuFile);
   if (doc.isNull())
   {
       kdWarning() << "Can't find menu file!" << endl;
       return true;
   }

   TQDomElement docElem = doc.documentElement();
   TQDomNode n = docElem.firstChild();
   TQDomNode next;
   for(; !n.isNull(); n = next ) 
   {
      TQDomElement e = n.toElement(); // try to convert the node to an element.
      next = n.nextSibling();

      if ((e.tagName() == "MergeFile") && (e.text() == "applications-kmenuedit.menu"))
         break;
   }
   TQDomNode insertionPoint = n;
   if (insertionPoint.isNull())
   {
      kdWarning() << "Application menu fails to include applications-kmenuedit.menu" << endl;
      return false;
   }
   TQDomElement docChangesElem = docChanges.documentElement();
   n = docChangesElem.firstChild();
   for(; !n.isNull(); n = next ) 
   {
      TQDomElement e = n.toElement(); // try to convert the node to an element.
      next = n.nextSibling();

      docElem.insertBefore(n, insertionPoint);
   }

   KTempFile tempFile;
   tempFile.close();

   saveDoc(tempFile.name(), doc);
   result = KioskRun::self()->install(tempFile.name(), menuFileSave);
   if (!result) return false;
   

   // Install .desktop files
   {
      TQString legacyApplications = KioskRun::self()->locateLocal("apps", TQString());
      TQString legacySaveApplications = KioskRun::self()->locateSave("apps", TQString());
      
      KioskSync legacyDir(kapp->mainWidget());
      legacyDir.addDir(legacyApplications, KURL());

      TQStringList newLegacyApplications = legacyDir.listFiles();
   
      for(TQStringList::ConstIterator it = newLegacyApplications.begin();
          it != newLegacyApplications.end(); ++it)
      {
         if ((*it).endsWith(".desktop") || (*it).endsWith(".kdelnk") || (*it).endsWith(".directory"))
         {
            kdDebug() << "MenueditComponent: New legacy file %s" << (legacyApplications+(*it)) << endl;
            result = KioskRun::self()->install(legacyApplications+(*it), legacySaveApplications+(*it));
            if (!result) return false;
         }
      }
   }

   // Install .desktop files
   {
      TQString xdgApplications = KioskRun::self()->locateLocal("xdgdata-apps", TQString());
      TQString xdgSaveApplications = KioskRun::self()->locateSave("xdgdata-apps", TQString());

      TQDir dir(xdgApplications);
      TQStringList newXdgApplications = dir.entryList(TQDir::All, TQDir::Unsorted);
      newXdgApplications.remove(".");
      newXdgApplications.remove("..");
   
      for(TQStringList::ConstIterator it = newXdgApplications.begin();
          it != newXdgApplications.end(); ++it)
      {
         if ((*it).endsWith(".desktop") || (*it).endsWith(".kdelnk"))
         {
            kdDebug() << "MenueditComponent: New .desktop file %s" << (xdgApplications+(*it)) << endl;
            result = KioskRun::self()->install(xdgApplications+(*it), xdgSaveApplications+(*it));
            if (!result) return false;
         }
      }
   }

   // Install .directory files
   {
      TQString xdgDirectories = KioskRun::self()->locateLocal("xdgdata-dirs", TQString());
      TQString xdgSaveDirectories = KioskRun::self()->locateSave("xdgdata-dirs", TQString());

      TQDir dir(xdgDirectories);
      TQStringList newXdgDirectories = dir.entryList(TQDir::All, TQDir::Unsorted);
      newXdgDirectories.remove(".");
      newXdgDirectories.remove("..");

      for(TQStringList::ConstIterator it = newXdgDirectories.begin();
          it != newXdgDirectories.end(); ++it)
      {
         if ((*it).endsWith(".directory"))
         {
            kdDebug() << "MenueditComponent: New .directory file %s" << (xdgDirectories+(*it)) << endl;
            result = KioskRun::self()->install(xdgDirectories+(*it), xdgSaveDirectories+(*it));
            if (!result) return false;
         }
      }
   }

   KioskRun::self()->forceSycocaUpdate();

   return true;
}

void
MenuEditComponent::slotPreviewStarted()
{
   KioskRun::self()->dcopRef("kicker", "kicker").call("showKMenu");
}


#include "menueditComponent.moc"