summaryrefslogtreecommitdiffstats
path: root/smb4k/core/smb4kbookmarkhandler.cpp
blob: 083cb531a65c86b8a0ed63d1235fe54253de0a3f (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/***************************************************************************
    smb4kbookmarkhandler  -  This class handles the bookmarks.
                             -------------------
    begin                : Fr Jan 9 2004
    copyright            : (C) 2004 by Alexander Reinholdt
    email                : dustpuppy@mail.berlios.de
 ***************************************************************************/

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

// TQt includes
#include <tqdir.h>
#include <tqfile.h>

// KDE includes
#include <kstandarddirs.h>
#include <kdebug.h>
#include <kapplication.h>

// system specific includes
#include <stdlib.h>
#include <math.h>

// application specific includes
#include "smb4kbookmarkhandler.h"
#include "smb4kdefs.h"
#include "smb4kerror.h"
#include "smb4kglobal.h"
#include "smb4knetworkitems.h"
#include "smb4kbookmark.h"

using namespace Smb4TDEGlobal;


Smb4KBookmarkHandler::Smb4KBookmarkHandler( TQValueList<Smb4KHostItem *> *hosts,
TQObject *parent, const char *name )
: TQObject( parent, name ), m_hosts( hosts )
{
  // First we need the directory.
  TDEStandardDirs *stddir = new TDEStandardDirs();
  TQString dir = locateLocal( "data", "smb4k", TDEGlobal::instance() );

  if ( !stddir->exists( dir ) )
  {
    stddir->makeDir( dir );
  }

  delete stddir;

  loadBookmarks();
}


Smb4KBookmarkHandler::~Smb4KBookmarkHandler()
{
  for ( TQValueList<Smb4KBookmark *>::Iterator it = m_bookmarks.begin(); it != m_bookmarks.end(); ++it )
  {
    delete *it;
  }

  m_bookmarks.clear();

  // Do not delete m_hosts here, because it is either NULL
  // or it is handled outside of this class.
}



void Smb4KBookmarkHandler::addBookmark( Smb4KBookmark *bookmark )
{
  if ( !bookmark )
  {
    return;
  }

  if ( TQString::compare( bookmark->type(), "Printer" ) == 0 )
  {
    Smb4KError::error( ERROR_BOOKMARK_PRINTER );
    return;
  }

  if ( TQString::compare( bookmark->share(), "homes" ) == 0 )
  {
    TQString share = specifyUser( bookmark->host(), kapp->mainWidget() ? kapp->mainWidget() : 0, "SpecifyUser" );

    bookmark->setShareName( share );
  }

  // Search for the bookmark:
  Smb4KBookmark *result = findBookmarkByName( bookmark->bookmark() );

  if ( result )
  {
    if ( TQString::compare( result->workgroup().upper(), bookmark->workgroup().upper() ) == 0 )
    {
      // It's the same bookmark. We'll update it in a very
      // brutal but efficient way:
      m_bookmarks.remove( result );

      if ( result )
      {
        delete result;
      }
    }

    m_bookmarks.append( bookmark );
  }
  else
  {
    // The bookmark is new. Append it to the list:
    m_bookmarks.append( bookmark );
  }

  writeBookmarkList( m_bookmarks );
}


void Smb4KBookmarkHandler::writeBookmarkList( const TQValueList<Smb4KBookmark *> &list )
{
  if ( list != m_bookmarks )
  {
    for ( TQValueListIterator<Smb4KBookmark *> it = m_bookmarks.begin(); it != m_bookmarks.end(); ++it )
    {
      delete *it;
    }

    m_bookmarks.clear();

    m_bookmarks = list;
  }

  TQFile file( locateLocal( "data", "smb4k/bookmarks", TDEGlobal::instance() ) );

  if ( file.open( IO_WriteOnly ) )
  {
    TQTextStream ts( &file );
    ts.setEncoding( TQTextStream::Locale );

    int serial_number = 0;

    for ( TQValueListConstIterator<Smb4KBookmark *> it = m_bookmarks.begin(); it != m_bookmarks.end(); ++it )
    {
      if ( !(*it)->label().isEmpty() )
      {
        Smb4KBookmark *result = findBookmarkByLabel( (*it)->label() );

        if ( result &&
             (TQString::compare( result->bookmark().upper(), (*it)->bookmark().upper() ) != 0 ||
             TQString::compare( result->workgroup().upper(), (*it)->workgroup().upper() ) != 0) )
        {
          Smb4KError::information( INFO_BOOKMARK_LABEL_IN_USE, (*it)->label(), (*it)->bookmark() );

          (*it)->setLabel( TQString( "%1 (%2)" ).arg( (*it)->label() ).arg( serial_number++ ) );
        }
      }

      ts << (*it)->host() << ","
         << (*it)->share() << ","
         << (*it)->workgroup() << ","
         << (*it)->ip() << ","
         << (*it)->label() << endl;
    }

    file.close();
  }
  else
  {
    Smb4KError::error( ERROR_WRITING_FILE, TQDir::currentDirPath()+"/"+file.name() );
    return;
  }

  emit bookmarksUpdated();
}


void Smb4KBookmarkHandler::loadBookmarks()
{
  TQFile file( locateLocal( "data", "smb4k/bookmarks", TDEGlobal::instance() ) );

  TQStringList contents;

  if ( file.open( IO_ReadOnly ) )
  {
    TQTextStream ts( &file );
    ts.setEncoding( TQTextStream::Locale );

    contents = TQStringList::split( '\n', ts.read(), false );

    file.close();

    for ( TQStringList::ConstIterator it = contents.begin(); it != contents.end(); ++it )
    {
      if ( (*it).startsWith( "#" ) || (*it).startsWith( "[" ) ||
         TQString::compare( (*it).stripWhiteSpace(), TQString() ) == 0 )
      {
        continue;
      }
      else
      {
        // Load old bookmark entries (prior to version 0.7.0)
        if ( ((*it).stripWhiteSpace())[0].isDigit() )
        {
          TQString bookmark = (*it).section( "=", 1, -1 ).stripWhiteSpace();
          m_bookmarks.append( new Smb4KBookmark( bookmark.section( "/", 2, 2 ).stripWhiteSpace(), bookmark.section( "/", 3, 3 ).stripWhiteSpace(), TQString(), TQString(), "Disk" ) );
        }
        else
        {
          TQString host = (*it).section( ",", 0, 0 ).stripWhiteSpace();
          TQString share = (*it).section( ",", 1, 1 ).stripWhiteSpace();
          TQString workgroup = (*it).section( ",", 2, 2 ).stripWhiteSpace();
          TQString ip = (*it).section( ",", 3, 3 ).stripWhiteSpace();
          TQString label = (*it).section( ",", 4, 4 ).stripWhiteSpace();

          m_bookmarks.append( new Smb4KBookmark( host, share, workgroup, ip, "Disk", label ) );
        }
      }
    }

    emit bookmarksUpdated();
  }
  else
  {
    if ( file.exists() )
    {
      Smb4KError::error( ERROR_READING_FILE, file.name() );
    }
    else
    {
      // Do nothing if the file does not exist.
    }
  }
}


Smb4KBookmark *Smb4KBookmarkHandler::findBookmarkByName( const TQString &bookmark )
{
  // Update the bookmarks:
  update();

  // Find the bookmark:
  TQValueListConstIterator<Smb4KBookmark *> it;

  for ( it = m_bookmarks.begin(); it != m_bookmarks.end(); ++it )
  {
    if ( TQString::compare( (*it)->bookmark().upper(), bookmark.upper() ) == 0 )
    {
      break;
    }
  }

  return it != m_bookmarks.end() ? *it : NULL;
}


Smb4KBookmark *Smb4KBookmarkHandler::findBookmarkByLabel( const TQString &label )
{
  // Update the bookmarks:
  update();

  // Find the bookmark:
  TQValueListConstIterator<Smb4KBookmark *> it;

  for ( it = m_bookmarks.begin(); it != m_bookmarks.end(); ++it )
  {
    if ( TQString::compare( (*it)->label().upper(), label.upper() ) == 0 )
    {
      break;
    }
  }

  return it != m_bookmarks.end() ? *it : NULL;
}


const TQValueList<Smb4KBookmark *> &Smb4KBookmarkHandler::getBookmarks()
{
  // Update the bookmarks:
  update();

  // Return the list of bookmarks:
  return m_bookmarks;
}


void Smb4KBookmarkHandler::update()
{
  // If the user didn't pass the global Smb4KHostItem list, we are not able
  // to update the bookmarks:
  if ( !m_hosts )
  {
    return;
  }

  // Search the list of hosts for new IP addresses:
  for ( TQValueList<Smb4KBookmark *>::Iterator it = m_bookmarks.begin(); it != m_bookmarks.end(); ++it )
  {
    for ( TQValueList<Smb4KHostItem *>::ConstIterator i = m_hosts->begin(); i != m_hosts->end(); ++i )
    {
      if ( TQString::compare( (*it)->workgroup().lower(), (*i)->workgroup().lower() ) != 0 )
      {
        // Continue, if the workgroup is not the same:
        continue;
      }
      else
      {
        if ( TQString::compare( (*it)->host().lower(), (*i)->name().lower() ) != 0 )
        {
          // Continue if the host name is not the same:
          continue;
        }
        else
        {
          // Set the IP address if it changed:
          if ( !(*i)->ip().stripWhiteSpace().isEmpty() &&
               TQString::compare( (*it)->ip(), (*i)->ip() ) != 0 )
          {
            (*it)->setIP( (*i)->ip() );
          }

          break;
        }
      }
    }
  }
}

#include "smb4kbookmarkhandler.moc"