summaryrefslogtreecommitdiffstats
path: root/smb4k/searchdlg/smb4ksearchdialog_part.cpp
blob: d1eb2f8fd2545b29ea678afc6265ae37bf4979c7 (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
/***************************************************************************
    smb4ksearchdialog_part  -  This Part encapsulates the search dialog
    of Smb4K.
                             -------------------
    begin                : Fr Jun 1 2007
    copyright            : (C) 2007 by Alexander Reinholdt
    email                : dustpuppy@users.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 <tqimage.h>
#include <tqpixmap.h>

// KDE includes
#include <tdeaboutdata.h>
#include <kinstance.h>
#include <kdebug.h>
#include <kiconeffect.h>
#include <kiconloader.h>
#include <kcombobox.h>

// application specific includes
#include "smb4ksearchdialog_part.h"
#include "smb4ksearchdialog.h"
#include "smb4ksearchdialogitem.h"
#include "../core/smb4kcore.h"
#include "../core/smb4knetworkitems.h"
#include "../core/smb4kdefs.h"

TDEInstance *Smb4KSearchDialogPartFactory::m_instance = 0L;
TDEAboutData *Smb4KSearchDialogPartFactory::m_about = 0L;


Smb4KSearchDialogPart::Smb4KSearchDialogPart( TQWidget *parentWidget, const char *widgetName,
                                              TQObject *parent, const char *name )
: KParts::Part( parent, name )
{
  // First of all we need an instance:
  setInstance( Smb4KSearchDialogPartFactory::instance() );

  // Set the XML file:
//   setXMLFile( "smb4ksearchdialog_part.rc" );

  // Set the widget of this part:
  m_widget = new Smb4KSearchDialog( parentWidget, widgetName );
  setWidget( m_widget );

  m_serial_number = 0;

  // Connections:
  connect( m_widget,              TQ_SIGNAL( buttonPressed( int ) ),
           this,                  TQ_SLOT( slotButtonPressed( int ) ) );

  connect( m_widget->listView(),  TQ_SIGNAL( doubleClicked( TQListViewItem * ) ),
           this,                  TQ_SLOT( slotItemDoubleClicked( TQListViewItem * ) ) );

  connect( Smb4KCore::scanner(),  TQ_SIGNAL( searchResult( Smb4KHostItem * ) ),
           this,                  TQ_SLOT( slotReceivedSearchResult( Smb4KHostItem * ) ) );

  connect( Smb4KCore::scanner(),  TQ_SIGNAL( hostListChanged() ),
           this,                  TQ_SLOT( slotCheckItemIsKnown() ) );
}


Smb4KSearchDialogPart::~Smb4KSearchDialogPart()
{
}


void Smb4KSearchDialogPart::customEvent( TQCustomEvent *e )
{
  switch ( e->type() )
  {
    case EVENT_LOAD_SETTINGS:
    {
      // Not needed at the moment.

      break;
    }
    case EVENT_SET_FOCUS:
    {
      m_widget->toolBar()->getCombo( Smb4KSearchDialog::Combo )->lineEdit()->setFocus();

      break;
    }
    default:
    {
      break;
    }
  }

  KParts::Part::customEvent( e );
}


/////////////////////////////////////////////////////////////////////////////
// SLOT IMPLEMENTATIONS (Smb4KSearchDialogPart)
/////////////////////////////////////////////////////////////////////////////

void Smb4KSearchDialogPart::slotButtonPressed( int button_id )
{
  switch ( button_id )
  {
    case Smb4KSearchDialog::Search:
    {
      Smb4KCore::scanner()->search( m_widget->searchString() );

      break;
    }
    case Smb4KSearchDialog::Add:
    {
      Smb4KSearchDialogItem *search_item = static_cast<Smb4KSearchDialogItem *>( m_widget->listView()->currentItem() );

      if ( search_item && !search_item->isKnown() )
      {
        Smb4KCore::scanner()->insertHost( search_item->hostItem() );
      }
      else
      {
        // Do nothing
      }

      break;
    }
    default:
    {
      break;
    }
  }
}


void Smb4KSearchDialogPart::slotReceivedSearchResult( Smb4KHostItem *item )
{
  if ( item )
  {
    // Create a Smb4KSearchDialogItem. This will also add it
    // to the list box.
    (void) new Smb4KSearchDialogItem( m_widget->listView(), item, m_serial_number++ );

    // Enable the combo box and set the focus:
    m_widget->toolBar()->setItemEnabled( Smb4KSearchDialog::Combo, true );
    m_widget->toolBar()->getCombo( Smb4KSearchDialog::Combo )->setFocus();

    // Now select the text, so that the user can easily
    // remove it.
    int string_length = m_widget->toolBar()->getCombo( Smb4KSearchDialog::Combo )->lineEdit()->text().length();
    m_widget->toolBar()->getCombo( Smb4KSearchDialog::Combo )->lineEdit()->setSelection( 0, string_length );

    slotCheckItemIsKnown();
  }
}


void Smb4KSearchDialogPart::slotCheckItemIsKnown()
{
  TQListViewItemIterator it( m_widget->listView() );

  while ( it.current() )
  {
    Smb4KSearchDialogItem *item = static_cast<Smb4KSearchDialogItem *>( it.current() );

    if ( item && item->isRegular() )
    {
      Smb4KHostItem *host = Smb4KCore::scanner()->getHost( item->hostItem()->name(), item->hostItem()->workgroup() );

      item->setKnown( (host ? true : false) );
    }
    else
    {
      // Do nothing
    }

    ++it;
  }
}


void Smb4KSearchDialogPart::slotItemDoubleClicked( TQListViewItem *item )
{
  if ( item )
  {
    // If we have got an item, enable the "Add" button if the
    // item is regular. Otherwise disable the button.
    Smb4KSearchDialogItem *search_item = static_cast<Smb4KSearchDialogItem *>( item );

    if ( search_item && search_item->isRegular() && !search_item->isKnown() )
    {
      Smb4KCore::scanner()->insertHost( search_item->hostItem() );
    }
    else
    {
      // Do nothing
    }
  }
}


/////////////////////////////////////////////////////////////////////////////
// FACTORY STUFF
/////////////////////////////////////////////////////////////////////////////

Smb4KSearchDialogPartFactory::Smb4KSearchDialogPartFactory()
: KParts::Factory()
{
}


Smb4KSearchDialogPartFactory::~Smb4KSearchDialogPartFactory()
{
  delete m_instance;
  delete m_about;

  m_instance = 0L;
}


KParts::Part *Smb4KSearchDialogPartFactory::createPartObject( TQWidget *parentWidget, const char *widgetName,
TQObject *parent, const char *name, const char *, const TQStringList & )
{
  Smb4KSearchDialogPart *obj = new Smb4KSearchDialogPart( parentWidget, widgetName, parent, name );

  // See if we are to be read-write or not
//   if (TQCString(classname) == "KParts::ReadOnlyPart")
//   {
//     obj->setReadWrite(false);
//   }

  return obj;
}


TDEInstance *Smb4KSearchDialogPartFactory::instance()
{
  if( !m_instance )
  {
    m_about = new TDEAboutData( "smb4ksearchdialogpart", I18N_NOOP( "Smb4KSearchDialogPart" ), "1.0" );
    m_about->addAuthor("Alexander Reinholdt", 0, "dustpuppy@users.berlios.de");
    m_about->setLicense( TDEAboutData::License_GPL );
    m_instance = new TDEInstance( m_about );
  }

  return m_instance;
}


/////////////////////////////////////////////////////////////////////////////
// INIT
/////////////////////////////////////////////////////////////////////////////

extern "C"
{
  TDE_EXPORT void *init_libsmb4ksearchdialog()
  {
    TDEGlobal::locale()->insertCatalogue( "smb4k" );
    return new Smb4KSearchDialogPartFactory;
  }
}

#include "smb4ksearchdialog_part.moc"