summaryrefslogtreecommitdiffstats
path: root/quanta/components/debugger/pathmapper.cpp
blob: e1d7487565dd8d00ccef49714e1fad5d0cea453e (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
/***************************************************************************
                               pathmapper.h
                         ------------------------
    begin                : 2004-10-10
    copyright            : (C) 2004 Linus McCabe

 ***************************************************************************/

/****************************************************************************
 *                                                                          *
 *   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.                                    *
 *                                                                          *
 ***************************************************************************/

#include "pathmapper.h"
#include "pathmapperdialog.h"

#include "quantacommon.h"
#include "project.h"

#include <kdebug.h>
#include <klocale.h>
#include <tqstring.h>
#include <qextfileinfo.h>
#include <tqdom.h>

PathMapper::PathMapper(TQObject *tqparent, const char *name)
    : TQObject(tqparent, name)
{

}

PathMapper::PathMapper()
{

}

TQString PathMapper::translate(const TQString & path, const TQString & from, const TQString &to)
{
  TQString translated = path;
  
  // Check if this dir is matched by the maps
  if(path.tqstartsWith(from, false))
  {
    translated.remove(0, from.length());
    translated = to + translated;
  }

  return translated;
}

TQString PathMapper::mapLocalPathToServer(const TQString &localpath)
{
  if(localpath.isEmpty())
    return localpath;
    
  if(m_localBasedir == "/" && m_serverBasedir == "/")
    return localpath;
    
  TQString newpath = translate(localpath, m_localBasedir, m_serverBasedir);
  
  // If no translation occurred, check and see if some of the other translations does
  if(newpath == localpath)
  {
    for (unsigned int cnt = 0; cnt < m_serverlist.count(); cnt++ )
    {
      // If the entire filename matches, count it as a match even if there is no difference
      if(m_locallist[cnt] == localpath)
        return localpath;

      // If both are slashes, count it as a local project
      if(m_locallist[cnt] == "/" && m_serverlist[cnt] == "/")
        return localpath;

      // Check if it translates into something 
      newpath = translate(localpath, m_locallist[cnt], m_serverlist[cnt]);
      if(newpath != localpath)
      {
        addHistory(localpath, newpath, true);
        return newpath;
      }
    }

    // No translation found -> show dialog
    PathMapperDialog pmdlg(localpath, PathMapperDialog::LocalToServer);
    for (unsigned int cnt = 0; cnt < m_serverlist.count(); cnt++ )
      pmdlg.addHistory(m_serverlist[cnt], m_locallist[cnt]);

    if(pmdlg.exec() == TQDialog::Accepted )
    {
      newpath = translate(localpath, pmdlg.localPath(), pmdlg.serverPath());
      addHistory(pmdlg.localPath(), pmdlg.serverPath(), newpath != localpath); 
      
      return newpath;
    }
    return localpath;
  }
  return newpath;
}

TQString PathMapper::mapServerPathToLocal(const TQString &serverpath)
{
  TQString newpath;
  if(serverpath.isEmpty())
    return serverpath;

  newpath = translate(serverpath, m_serverBasedir, m_localBasedir);
  
  // Check if this dir is matched by the basedirs
  if(TQExtFileInfo::exists(newpath, true, 0L))
    return newpath;

  // Check if any previous mappings fit...
  for (unsigned int cnt = 0; cnt < m_serverlist.count(); cnt++ )
  {
    newpath = translate(serverpath, m_serverlist[cnt], m_locallist[cnt]);
    if(TQExtFileInfo::exists(newpath, true, 0L))
      return newpath;
  }
  
  // If the basedirs didnt match, check if the file exists,
  // otherwise scan through the mapping history or show the
  // mapping dialog
  if(!TQExtFileInfo::exists(serverpath, true, 0L))
  {
    PathMapperDialog pmdlg(serverpath, PathMapperDialog::ServerToLocal);
    for (unsigned int cnt = 0; cnt < m_serverlist.count(); cnt++ )
      pmdlg.addHistory(m_serverlist[cnt], m_locallist[cnt]);

    if(pmdlg.exec() == TQDialog::Accepted )
    {
      addHistory(pmdlg.localPath(), pmdlg.serverPath(), true);
      newpath = translate(serverpath, pmdlg.localPath(), pmdlg.serverPath());
      return newpath;
    }
  }

  return serverpath;
}

void PathMapper::setLocalBasedir(const TQString &localpath)
{
  m_localBasedir = localpath;
}
void PathMapper::setServerBasedir(const TQString &serverpath)
{
  m_serverBasedir = serverpath;
}

TQDomNode PathMapper::pathMapperNode()
{
  TQDomNode nodeThisDbg;
  TQDomDocument *dom = Project::ref()->sessionDom();
  TQDomNode projectNode = dom->firstChild().firstChild();
  TQDomNode nodeDbg  = projectNode.namedItem("debuggers");
  if(nodeDbg.isNull())
  {
    nodeDbg = dom->createElement("debuggers");
    projectNode.appendChild(nodeDbg);
  }

  // Find the pathmapper section
  nodeThisDbg = nodeDbg.namedItem("pathmapper");
  if(nodeThisDbg.isNull())
  {
    nodeThisDbg = dom->createElement("pathmapper");
    nodeDbg.appendChild(nodeThisDbg);
  }
  
  return nodeThisDbg;
}

void PathMapper::addHistory(const TQString &localpath, const TQString &serverpath, bool saveinproject)
{
  bool exists = false;
  for (unsigned int cnt = 0; cnt < m_serverlist.count() && !exists; cnt++ )
    if(m_serverlist[cnt] == serverpath &&  m_locallist[cnt] == localpath)
      exists = true;

  if(!exists)
  {
    if(saveinproject)
    {
      TQDomNode node = pathMapperNode();
      TQDomNode newnode = Project::ref()->dom()->createElement("mapping");
  
      TQDomAttr serverattr = Project::ref()->dom()->createAttribute("serverpath");
      serverattr.setValue(serverpath);
      TQDomAttr localattr = Project::ref()->dom()->createAttribute("localpath");
      localattr.setValue(localpath);
  
      newnode.attributes().setNamedItem(serverattr);
      newnode.attributes().setNamedItem(localattr);
  
      node = node.namedItem("mappings");
      node.insertAfter(newnode, node.lastChild());
    }
    
    m_serverlist.append(serverpath);
    m_locallist.append(localpath);
  }

}

void PathMapper::readConfig()
{
  TQDomNode node = pathMapperNode();

  // Server
  TQDomNode valuenode = node.namedItem("mappings");
  TQDomNode child = valuenode.firstChild();
  TQString serverpath, localpath;
  while(!child.isNull())
  {
    serverpath = child.attributes().namedItem("serverpath").nodeValue();
    localpath = child.attributes().namedItem("localpath").nodeValue();
    kdDebug(24002) << "PathMapper::readConfig " << serverpath << ", " << localpath << endl;

    m_serverlist.append(serverpath);
    m_locallist.append(localpath);
    child = child.nextSibling();
  }
}

#include "pathmapper.moc"