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
|
/*
**************************************************************************
description
--------------------
copyright : (C) 2003 by Luis Carvalho
email : lpassos@oninetspeed.pt
**************************************************************************
**************************************************************************
* *
* 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 "pmlibraryiconview.h"
#include <unistd.h>
#include <tqstring.h>
#include <tqdir.h>
#include <kurl.h>
#include <tdeio/job.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <stdlib.h>
#include "pmlibraryhandle.h"
#include "pmlibraryobject.h"
#include "pmdebug.h"
const char* PMLibraryIconDrag::format( int i ) const
{
switch( i )
{
case 0:
return "application/x-qiconlist";
break;
case 1:
return "text/sublib-list";
break;
default:
return 0;
break;
}
}
TQByteArray PMLibraryIconDrag::encodedData( const char* mime ) const
{
TQByteArray a;
if ( TQString( mime ) == "application/x-qiconlist" )
a = TQIconDrag::encodedData( mime );
else if ( TQString( mime ) == "text/sublib-list" )
{
TQString s , l;
for( unsigned i = 0; i < m_paths.count( ); ++i )
{
if( m_subLibs[i] )
l = "true";
else
l = "false";
s += m_paths[i] + "\r" + l + "\n";
}
a.resize( s.length( ) );
memcpy( a.data( ), s.latin1( ), s.length( ) );
}
return a;
}
bool PMLibraryIconDrag::canDecode( TQMimeSource* e )
{
return e->provides( "application/x-qiconlist" ) ||
e->provides( "text/sublib-list" );
}
bool PMLibraryIconDrag::decode( TQMimeSource* e, TQStringList& strList, TQValueList<bool>& subLibList )
{
TQByteArray a = e->encodedData( "text/sublib-list" );
if( a.isEmpty( ) )
return false;
TQStringList list = TQStringList::split( "\n", TQString( a ) );
for( unsigned i = 0; i < list.count( ); ++i )
{
strList.append( list[i].section( "\r", 0, 0 ) );
if( list[i].section( "\r", 1, 1 ) == "true" )
subLibList.append( true );
else
subLibList.append( false );
}
return true;
}
void PMLibraryIconDrag::append( const TQIconDragItem &item, const TQRect &pr,
const TQRect &tr, const TQString &path, bool isSubLibrary )
{
TQIconDrag::append( item, pr, tr );
m_paths << path;
m_subLibs.append( isSubLibrary );
}
PMLibraryIconView::PMLibraryIconView( TQWidget* parent, const char* name )
: TDEIconView( parent, name )
{
m_pLibrary = NULL;
m_pCurrentLibrary = NULL;
setSelectionMode( Single );
setMode( Execute );
}
void PMLibraryIconView::setLibrary( PMLibraryHandle* h )
{
m_pLibrary = h;
refresh( );
}
void PMLibraryIconView::refresh( )
{
// Clear all the icons
clear( );
PMLibraryHandle::EntryIterator* it;
// Scan all the library objects and load them into the view
// First let's add the libraries
it = m_pLibrary->createSubLibraryIterator( );
for( ; it->current( ); ++(*it) )
{
TQString f_name = *( it->current( ) );
PMLibraryHandle h( f_name );
new PMLibraryIconViewItem( this, h.name( ), f_name, true );
}
delete it;
// Then the objects
it = m_pLibrary->createObjectIterator( );
for( ; it->current( ); ++(*it) )
{
TQString f_name = *( it->current( ) );
PMLibraryObject obj( f_name );
if( obj.preview( ) )
new PMLibraryIconViewItem( this, obj.name( ), obj.preview( )->copy( ), f_name, false );
else
new PMLibraryIconViewItem( this, obj.name( ), f_name, false );
}
delete it;
}
void PMLibraryIconView::slotDropped( TQDropEvent *e, const TQValueList<TQIconDragItem> & )
{
e->ignore( );
}
TQDragObject* PMLibraryIconView::dragObject( )
{
if ( !currentItem( ) )
return 0;
TQPoint orig = viewportToContents( viewport( )->mapFromGlobal( TQCursor::pos( ) ) );
PMLibraryIconDrag *drag = new PMLibraryIconDrag( viewport( ) );
drag->setPixmap( *currentItem( )->pixmap( ),
TQPoint( currentItem( )->pixmapRect( ).width( ) / 2,
currentItem( )->pixmapRect( ).height( ) / 2 ) );
for ( PMLibraryIconViewItem *item = (PMLibraryIconViewItem*)firstItem( );
item; item = ( PMLibraryIconViewItem* )item->nextItem( ) )
{
if ( item->isSelected( ) )
{
TQIconDragItem id;
id.setData( TQCString( item->path( ).latin1( ) ) );
drag->append( id,
TQRect( item->pixmapRect( FALSE ).x( ) - orig.x( ),
item->pixmapRect( FALSE ).y( ) - orig.y( ),
item->pixmapRect( ).width( ),
item->pixmapRect( ).height( ) ),
TQRect( item->textRect( FALSE ).x( ) - orig.x( ),
item->textRect( FALSE ).y( ) - orig.y( ),
item->textRect().width( ),
item->textRect( ).height( ) ),
item->path( ),
item->isSubLibrary( ) );
}
}
return drag;
}
PMLibraryIconViewItem::PMLibraryIconViewItem( TQIconView *parent, const TQString &text, const TQString& path, bool isSubLibrary )
: TDEIconViewItem( parent, text )
{
m_path = path;
m_isSubLibrary = isSubLibrary;
}
PMLibraryIconViewItem::PMLibraryIconViewItem( TQIconView *parent, const TQString &text, const TQImage& image, const TQString& path, bool isSubLibrary )
: TDEIconViewItem( parent, text, image )
{
m_path = path;
m_isSubLibrary = isSubLibrary;
}
bool PMLibraryIconViewItem::acceptDrop( const TQMimeSource *e ) const
{
if ( m_isSubLibrary && e->provides( "text/sublib-list" ) )
return true;
return false;
}
void PMLibraryIconViewItem::dropped( TQDropEvent *e, const TQValueList<TQIconDragItem> & )
{
TQStringList pathList;
TQValueList<bool> subLibList;
if( m_isSubLibrary && PMLibraryIconDrag::decode( e, pathList, subLibList ) )
{
PMLibraryIconView* source = static_cast<PMLibraryIconView*>( e->source( )->parentWidget( ) );
PMLibraryHandle* parentLib = source->library( );
PMLibraryHandle newParentLib = PMLibraryHandle( m_path );
if ( parentLib->isReadOnly() || newParentLib.isReadOnly() )
{
e->ignore();
return;
}
for( unsigned i = 0; i < pathList.count( ); ++i )
{
bool success = true;
TQString path = pathList[i];
if( path != ( m_path +"/" + path.section( '/', -1 ) ) )
{
if( subLibList[i] )
{
TQString newpath = newPath( path, true );
if( parentLib->deleteSubLibrary( path ) == PMLibraryHandle::Ok )
{
PMLibraryHandle lib = PMLibraryHandle( path );
if( newParentLib.addSubLibrary( newpath, lib.name() ) == PMLibraryHandle::Ok )
{
lib.changeParentLibrary( m_path );
TDEIO::move( path, newpath );
}
else
{
success = false;
parentLib->addSubLibrary( path, lib.name( ) );
}
}
else
success = false;
}
else
{
TQString newpath = newPath( path, false );
if( parentLib->deleteObject( path ) == PMLibraryHandle::Ok )
{
PMLibraryObject obj = PMLibraryObject( path );
if( newParentLib.addObject( newpath, obj.name() ) == PMLibraryHandle::Ok )
{
TDEIO::move( path, newpath );
}
else
{
success = false;
parentLib->addObject( path, obj.name( ) );
}
}
else
success = false;
}
if( !success )
{
KMessageBox::error( 0, i18n( "Error moving \"%1\" to \"%2\"" ).arg( path ).arg( m_path ) );
e->ignore( );
return;
}
}
}
e->acceptAction( );
source->refresh( );
}
else
{
e->ignore( );
}
}
TQString PMLibraryIconViewItem::newPath( const TQString oldPath, bool /*subLib*/ )
{
/// @todo Need to replace mkdtemp and mkstemps before enabling libs
return oldPath;
/*
TQString path = m_path + "/" + oldPath.section( '/', -1 );
if( subLib )
{
TQString test = path + "/library_index.xml";
if( TQFile::exists( test ) )
{
TQCString s = m_path.latin1();
s+= "/libXXXXXX";
char* dirname = mkdtemp( s.data() );
rmdir( dirname );
path = dirname;
}
}
else if( TQFile::exists( path ) )
{
// we need to rename it.
TQCString s = m_path.latin1();
s += "/objXXXXXX.kpml";
int fh = mkstemps( s.data( ), 5 );
close( fh );
unlink( s.data() );
path = s;
}
return path;
*/
}
#include "pmlibraryiconview.moc"
|