summaryrefslogtreecommitdiffstats
path: root/libkipi/libkipi/uploadwidget.cpp
blob: 60f8744c74a9bc6cbed0c260951cb0fb445eb3e1 (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
/* ============================================================
 * File   : uploadwidget.cpp
 * Authors: KIPI team developers (see AUTHORS files for details)
 *	    
 * Date   : 2004-02
 * Description :
 *
 * Copyright 2004 by the KIPI team
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Library General
 * Public License as published by the Free Software Foundation;
 * either version 2, 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 Library General Public License for more details.
 *
 * ============================================================ */

// TQt includes. 
  
#include <tqlayout.h>
#include <tqheader.h>
#include <tqlistview.h>
#include <tqdir.h>

// KDE includes

#include <kdebug.h>
#include <klocale.h>
#include <tdeio/jobclasses.h>
#include <kmessagebox.h>

#include <tdeversion.h>
#if TDE_VERSION >= 0x30200
#include <kinputdialog.h>
#else
#include <klineeditdlg.h>
#define KInputDialog KLineEditDlg
#endif

// Local includes.

#include "uploadwidget.h"
#include "libkipi/imagecollection.h"


struct KIPI::UploadWidget::Private
{
    KFileTreeView* m_treeView;
    KFileTreeBranch* m_branch;
    TQStringList m_pendingPath;
};


/*!
  \class KIPI::UploadWidget
  This widget is used to specify an upload directory for new images.
*/

KIPI::UploadWidget::UploadWidget( KIPI::Interface* interface, TQWidget* parent, const char* name )
                  : TQWidget( parent, name )
{
    d = new Private;

    TQVBoxLayout* layout = new TQVBoxLayout( this, 0 );
    d->m_treeView = new KFileTreeView( this );
    d->m_treeView->setRootIsDecorated( true );
    layout->addWidget( d->m_treeView );

    // Fetch the current album, so we can start out there.
    KIPI::ImageCollection album = interface->currentAlbum();
    
    // If no current album selected, get the first album in the list.
    if ( !album.isValid() || !album.isDirectory() ) 
       album = interface->allAlbums().first();
    
    d->m_branch = d->m_treeView->addBranch( TQDir::cleanDirPath(album.uploadRoot().path()),
                                          album.uploadRootName() );
    d->m_treeView->setDirOnlyMode( d->m_branch, true );

    d->m_treeView->addColumn( i18n("Folder" ) );

    d->m_treeView->header()->setStretchEnabled( true, 0 );
    d->m_treeView->header()->hide();

    TQString root = album.uploadRoot().path();
    TQString uploadPath = album.isDirectory() ? album.uploadPath().path() : root;

    root       = TQDir::cleanDirPath(root);
    uploadPath = TQDir::cleanDirPath(uploadPath);
    
    if ( !uploadPath.startsWith( root ) ) 
    {
        kdWarning(51000) << "Error in Host application: uploadPath() should start with uploadRoot()." << endl
                         << "uploadPath() = " << album.uploadPath().prettyURL() << endl
                         << "uploadRoot() = " << album.uploadRoot().prettyURL() << endl;
    }
    else
    {
        uploadPath = uploadPath.mid( root.length() );
        
        d->m_pendingPath = TQStringList::split( "/", uploadPath, false );
        
        connect( d->m_branch, TQT_SIGNAL( populateFinished(KFileTreeViewItem *) ),
                 this, TQT_SLOT( slotPopulateFinished(KFileTreeViewItem *) ) );

        d->m_branch->setOpen(true);
    }
    
    connect( d->m_treeView, TQT_SIGNAL( executed(TQListViewItem *) ),
             this, TQT_SLOT( slotFolderSelected(TQListViewItem *) ) );
}

KIPI::UploadWidget::~UploadWidget()
{
    delete d;
}

KURL KIPI::UploadWidget::path() const
{
    return d->m_treeView->currentURL();
}

void KIPI::UploadWidget::load( )
{
    kdWarning() << "KIPI::UploadWidget::load(): This method is obsolete\n";
}

void KIPI::UploadWidget::slotPopulateFinished( KFileTreeViewItem * parentItem )
{
    if ( d->m_pendingPath.isEmpty() ) 
    {
        disconnect( d->m_branch, TQT_SIGNAL( populateFinished(KFileTreeViewItem *) ), 
                    this, TQT_SLOT( slotPopulateFinished(KFileTreeViewItem *) ) );
        return;
    }

    TQString itemName = d->m_pendingPath.front();

    d->m_pendingPath.pop_front();

    TQListViewItem * item;
    for ( item = parentItem->firstChild(); item; item = item->nextSibling() )
    {
        if ( item->text(0) == itemName )
        {
            break;
        }
    }
    
    if ( !item ) 
    {
        kdDebug( 51000 ) << "Unable to open " << itemName << endl;
    }
    else
    {
        item->setOpen( true );
        d->m_treeView->setSelected( item, true );
        d->m_treeView->ensureItemVisible ( item );
        
        KFileTreeViewItem * ftvItem = static_cast<KFileTreeViewItem *>( item );
        if ( ftvItem->alreadyListed() )
            slotPopulateFinished( ftvItem );
    }

}

void KIPI::UploadWidget::mkdir()
{
    if ( !path().isValid() ) 
        {
        KMessageBox::error( this, i18n("Please select a directory first.") );
        return;
        }

    bool ok;
    TQString dir = KInputDialog::getText( i18n("Create Directory"),
                                         i18n("<qt>Enter new directory name (to be created as subdir of %1):</qt>")
                                        .arg(path().prettyURL()), "", &ok, this);
    
    if (!ok) return;

    KURL url = path();
    url.addPath( dir );

    TDEIO::SimpleJob* job = TDEIO::mkdir(url);
    
    connect(job, TQT_SIGNAL(result(TDEIO::Job*)), 
            this, TQT_SLOT(slotAlbumCreated(TDEIO::Job*)));
}

void KIPI::UploadWidget::slotAlbumCreated(TDEIO::Job* job)
{
    int code = job->error();
    
    if ( code )
        job->showErrorDialog( this );
}

void KIPI::UploadWidget::slotFolderSelected(TQListViewItem *)
{
    emit folderItemSelected(d->m_treeView->currentURL());
}

#include "uploadwidget.moc"