summaryrefslogtreecommitdiffstats
path: root/smb4k/listview/smb4kshareslistview.cpp
blob: 95b828449005b2e25d15e82b02abaedac8d8acb5 (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
/***************************************************************************
    smb4kshareslistview  -  This is the shares list view of Smb4K.
                             -------------------
    begin                : Sa Jun 30 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 <tqtimer.h>

// KDE includes
#include <klocale.h>
#include <kurl.h>
#include <kio/job.h>
#include <kdeversion.h>
#include <kdebug.h>

// application specific includes
#include "smb4kshareslistview.h"
#include "smb4kshareslistviewitem.h"
#include "smb4kshareslistviewtooltip.h"
#include "../core/smb4ksettings.h"

Smb4KSharesListView::Smb4KSharesListView( TQWidget *tqparent, const char *name )
: KListView( tqparent, name )
{
  setSelectionModeExt( KListView::Single );
  setAllColumnsShowFocus( false );
  setItemsMovable( false );
  setAcceptDrops( true );

  // Set up columns
  addColumn( i18n( "Item" ) );
  addColumn( i18n( "Owner" ) );
#ifndef __FreeBSD__
  addColumn( i18n( "Login" ) );
#endif
  addColumn( i18n( "File System" ) );
  addColumn( i18n( "Free" ) );
  addColumn( i18n( "Used" ) );
  addColumn( i18n( "Total" ) );
  addColumn( i18n( "Usage" ) );

  // Set tqalignment
  setColumnAlignment( Free, TQt::AlignRight );
  setColumnAlignment( Used, TQt::AlignRight );
  setColumnAlignment( Total, TQt::AlignRight );
  setColumnAlignment( Usage, TQt::AlignRight );

  m_tooltip = NULL;

  // Connections:
  connect( this, TQT_SIGNAL( pressed( TQListViewItem * ) ),
           this, TQT_SLOT( slotPressed( TQListViewItem * ) ) );
}


Smb4KSharesListView::~Smb4KSharesListView()
{
  if ( m_tooltip )
  {
    delete m_tooltip;
  }
}


void Smb4KSharesListView::updateToolTip()
{
  if ( !m_tooltip )
  {
    return;
  }

  m_tooltip->update();
}


KURLDrag *Smb4KSharesListView::dragObject()
{
  // Get the KURL of the item that is to be dragged:
  KURL url = KURL( static_cast<Smb4KSharesListViewItem *>( currentItem() )->shareObject()->canonicalPath() );

  KURLDrag *drag = new KURLDrag( KURL::List( url ), this );
  drag->setPixmap( DesktopIcon( "folder" ) );
//   drag->dragCopy();

  return drag;
}


void Smb4KSharesListView::startDrag()
{
  if ( !Smb4KSettings::enableDragSupport() )
  {
    return;
  }

  KListView::startDrag();
}


void Smb4KSharesListView::contentsDragEnterEvent( TQDragEnterEvent *e )
{
  e->accept( Smb4KSettings::enableDropSupport() );
}


void Smb4KSharesListView::contentsDragMoveEvent( TQDragMoveEvent *e )
{
  TQListViewItem *item = itemAt( contentsToViewport( e->pos() ) );

  e->accept( Smb4KSettings::enableDropSupport() && item );
}


void Smb4KSharesListView::contentsDropEvent( TQDropEvent *e )
{
  TQListViewItem *item = itemAt( contentsToViewport( e->pos() ) );
  KURL::List src;

  // Do we have to stop here?
  if ( !Smb4KSettings::enableDropSupport() ||
       !item ||
       !KURLDrag::decode( e, src ) )
  {
    e->ignore();

    return;
  }

  KURL dest;
  dest.setPath( static_cast<Smb4KSharesListViewItem *>( item )->shareObject()->canonicalPath() );

  // Deny dropping if we dropped something on itself.
  // This was inspired by KonqOperations::doDrop() function.
  for ( KURL::List::Iterator it = src.begin(); it != src.end(); ++it )
  {
    if ( dest.equals( *it, true ) )
    {
      if ( e->source() == this || TQT_BASE_OBJECT(e->source()->tqparent()) == TQT_BASE_OBJECT(this) )
      {
        e->ignore();

        return;
      }
    }
  }

  // We only allow copying:
  KIO::CopyJob *job = KIO::copy( src, dest, true );
  job->setAutoErrorHandlingEnabled( true, NULL );
#if KDE_VERSION_MAJOR >= 3 && KDE_VERSION_MINOR >= 5
  job->setAutoWarningHandlingEnabled( true );
#endif
}


void Smb4KSharesListView::contentsMouseMoveEvent( TQMouseEvent *e )
{
  m_pos = e->globalPos();

  Smb4KSharesListViewItem *item = static_cast<Smb4KSharesListViewItem *>( itemAt( contentsToViewport( e->pos() ) ) );

  if ( item )
  {
    if ( m_tooltip )
    {
      // Check if tool tip is still valid:
      if ( m_tooltip->item() != item )
      {
        delete m_tooltip;

        if ( hasMouse() && Smb4KSettings::showShareToolTip() )
        {
          m_tooltip = new Smb4KSharesListViewToolTip( item );

          TQTimer::singleShot( 2000, this, TQT_SLOT( slotShowToolTip() ) );
        }
        else
        {
          m_tooltip = NULL;
        }
      }
      else
      {
        // Do nothing
      }

    }
    else
    {
      // Create new tool tip:
      if ( hasMouse() && Smb4KSettings::showShareToolTip() )
      {
        m_tooltip = new Smb4KSharesListViewToolTip( item );

        TQTimer::singleShot( 2000, this, TQT_SLOT( slotShowToolTip() ) );
      }
      else
      {
        // Do nothing
      }
    }
  }
  else
  {
    if ( m_tooltip )
    {
      delete m_tooltip;
      m_tooltip = NULL;
    }
  }

  KListView::contentsMouseMoveEvent( e );
}


/////////////////////////////////////////////////////////////////////////////
// TQT_SLOT IMPLEMENTATIONS
/////////////////////////////////////////////////////////////////////////////

void Smb4KSharesListView::slotPressed( TQListViewItem *item )
{
  if ( m_tooltip )
  {
    delete m_tooltip;
    m_tooltip = NULL;
  }

  if ( !item )
  {
    // Clear the selection if the user clicked onto the
    // viewport:
    clearSelection();
  }
  else
  {
    // Do nothing
  }
}


void Smb4KSharesListView::slotShowToolTip()
{
  if ( m_tooltip && hasMouse() && Smb4KSettings::showShareToolTip() &&
       (m_tooltip->item() == static_cast<Smb4KSharesListViewItem *>( itemAt( viewport()->mapFromGlobal( m_pos ) ) )) )
  {
    m_tooltip->showTip( m_pos );
  }
  else
  {
    delete m_tooltip;
    m_tooltip = NULL;
  }
}

#include "smb4kshareslistview.moc"