summaryrefslogtreecommitdiffstats
path: root/digikam/utilities/setup/setupcamera.cpp
blob: 1b8d0bb8c584c33dd4a2ddd19b108e1bf2564888 (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
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2003-02-10
 * Description : camera setup tab.
 * 
 * Copyright (C) 2003-2005 by Renchi Raju <[email protected]>
 * Copyright (C) 2006-2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * 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, 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.
 * 
 * ============================================================ */

// TQt includes.

#include <tqgroupbox.h>
#include <tqpushbutton.h>
#include <tqlayout.h>
#include <tqwhatsthis.h>
#include <tqtooltip.h>
#include <tqdatetime.h>
#include <tqlistview.h>

// KDE includes.

#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kurllabel.h>
#include <kiconloader.h>
#include <tdeglobalsettings.h>
#include <kstandarddirs.h>
#include <kcursor.h>
#include <tdeapplication.h>

// Local includes.

#include "cameraselection.h"
#include "cameralist.h"
#include "cameratype.h"
#include "gpcamera.h"
#include "setupcamera.h"
#include "setupcamera.moc"

namespace Digikam
{
class SetupCameraPriv
{
public:

    SetupCameraPriv()
    {
        listView         = 0;
        addButton        = 0;
        removeButton     = 0;        
        editButton       = 0;
        autoDetectButton = 0;
    }

    TQPushButton *addButton;
    TQPushButton *removeButton;
    TQPushButton *editButton;
    TQPushButton *autoDetectButton;
    
    TQListView   *listView;
};

SetupCamera::SetupCamera( TQWidget* parent )
           : TQWidget( parent )
{
    d = new SetupCameraPriv;

    TQVBoxLayout *mainLayout     = new TQVBoxLayout(parent);
    TQGridLayout* groupBoxLayout = new TQGridLayout( this, 2, 5, 0, KDialog::spacingHint() );

    d->listView = new TQListView( this );
    d->listView->addColumn( i18n("Title") );
    d->listView->addColumn( i18n("Model") );
    d->listView->addColumn( i18n("Port") );
    d->listView->addColumn( i18n("Path") );
    d->listView->addColumn( "Last Access Date", 0 ); // No i18n here. Hidden column with the last access date.
    d->listView->setAllColumnsShowFocus(true);
    TQWhatsThis::add( d->listView, i18n("<p>Here you can see the digital camera list used by digiKam "
                                       "via the Gphoto interface."));

    // -------------------------------------------------------------

    d->addButton        = new TQPushButton( this );
    d->removeButton     = new TQPushButton( this );
    d->editButton       = new TQPushButton( this );
    d->autoDetectButton = new TQPushButton( this );

    d->addButton->setText( i18n( "&Add..." ) );
    d->addButton->setIconSet(SmallIcon("add"));
    d->removeButton->setText( i18n( "&Remove" ) );
    d->removeButton->setIconSet(SmallIcon("remove"));
    d->editButton->setText( i18n( "&Edit..." ) );
    d->editButton->setIconSet(SmallIcon("configure"));
    d->autoDetectButton->setText( i18n( "Auto-&Detect" ) );
    d->autoDetectButton->setIconSet(SmallIcon("edit-find"));
    d->removeButton->setEnabled(false);
    d->editButton->setEnabled(false);

    TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );

    KURLLabel *gphotoLogoLabel = new KURLLabel(this);
    gphotoLogoLabel->setText(TQString());
    gphotoLogoLabel->setURL("http://www.gphoto.org");
    TDEGlobal::dirs()->addResourceType("logo-gphoto", TDEGlobal::dirs()->kde_default("data") + "digikam/data");
    TQString directory = TDEGlobal::dirs()->findResourceDir("logo-gphoto", "logo-gphoto.png");
    gphotoLogoLabel->setPixmap( TQPixmap( directory + "logo-gphoto.png" ) );
    TQToolTip::add(gphotoLogoLabel, i18n("Visit Gphoto project website"));

    groupBoxLayout->setAlignment( TQt::AlignTop );
    groupBoxLayout->addMultiCellWidget( d->listView, 0, 5, 0, 0 );
    groupBoxLayout->addWidget( d->addButton, 0, 1 );
    groupBoxLayout->addWidget( d->removeButton, 1, 1 );
    groupBoxLayout->addWidget( d->editButton, 2, 1 );
    groupBoxLayout->addWidget( d->autoDetectButton, 3, 1 );
    groupBoxLayout->addItem( spacer, 4, 1 );
    groupBoxLayout->addWidget( gphotoLogoLabel, 5, 1 );

    adjustSize();
    mainLayout->addWidget(this);

    // -------------------------------------------------------------

    connect(gphotoLogoLabel, TQ_SIGNAL(leftClickedURL(const TQString&)),
            this, TQ_SLOT(processGphotoURL(const TQString&)));

    connect(d->listView, TQ_SIGNAL(selectionChanged()),
            this, TQ_SLOT(slotSelectionChanged()));

    connect(d->addButton, TQ_SIGNAL(clicked()),
            this, TQ_SLOT(slotAddCamera()));

    connect(d->removeButton, TQ_SIGNAL(clicked()),
            this, TQ_SLOT(slotRemoveCamera()));

    connect(d->editButton, TQ_SIGNAL(clicked()),
            this, TQ_SLOT(slotEditCamera()));

    connect(d->autoDetectButton, TQ_SIGNAL(clicked()),
            this, TQ_SLOT(slotAutoDetectCamera()));

    // Add cameras --------------------------------------

    CameraList* clist = CameraList::instance();
    
    if (clist) 
    {
        TQPtrList<CameraType>* cl = clist->cameraList();

        for (CameraType *ctype = cl->first(); ctype;
             ctype = cl->next()) 
        {
            new TQListViewItem(d->listView, ctype->title(), ctype->model(),
                              ctype->port(), ctype->path(), 
                              ctype->lastAccess().toString(TQt::ISODate));
        }
    }
}

SetupCamera::~SetupCamera()
{
    delete d;
}

void SetupCamera::processGphotoURL(const TQString& url)
{
    TDEApplication::kApplication()->invokeBrowser(url);
}

void SetupCamera::slotSelectionChanged()
{
    TQListViewItem *item = d->listView->selectedItem();

    if (!item) 
    {
        d->removeButton->setEnabled(false);
        d->editButton->setEnabled(false);
        return;
    }

    d->removeButton->setEnabled(true);
    d->editButton->setEnabled(true);
}

void SetupCamera::slotAddCamera()
{
    CameraSelection *select = new CameraSelection;

    connect(select, TQ_SIGNAL(signalOkClicked(const TQString&, const TQString&, 
                                           const TQString&, const TQString&)),
            this,   TQ_SLOT(slotAddedCamera(const TQString&, const TQString&, 
                                         const TQString&, const TQString&)));

    select->show();
}

void SetupCamera::slotRemoveCamera()
{
    TQListViewItem *item = d->listView->currentItem();
    if (!item) return;

    delete item;
}

void SetupCamera::slotEditCamera()
{
    TQListViewItem *item = d->listView->currentItem();
    if (!item) return;

    CameraSelection *select = new CameraSelection;
    select->setCamera(item->text(0), item->text(1), item->text(2), item->text(3));

    connect(select, TQ_SIGNAL(signalOkClicked(const TQString&, const TQString&, 
                                           const TQString&, const TQString&)),
            this,   TQ_SLOT(slotEditedCamera(const TQString&, const TQString&, 
                                          const TQString&, const TQString&)));

    select->show();
}

void SetupCamera::slotAutoDetectCamera()
{
    TQString model, port;
    
    kapp->setOverrideCursor( KCursor::waitCursor() );
    int ret = GPCamera::autoDetect(model, port);
    kapp->restoreOverrideCursor();
    
    if (ret != 0) 
    {
        KMessageBox::error(this,i18n("Failed to auto-detect camera.\n"
                                     "Please check if your camera is turned on "
                                     "and retry or try setting it manually."));
        return;
    }

    // NOTE: See note in digikam/digikam/cameralist.cpp
    if (port.startsWith("usb:"))
    port = "usb:";
    
    if (d->listView->findItem(model, 1))
    {
        KMessageBox::information(this, i18n("Camera '%1' (%2) is already in list.").arg(model).arg(port));
    }
    else 
    {
        KMessageBox::information(this, i18n("Found camera '%1' (%2) and added it to the list.")
                                 .arg(model).arg(port));
        new TQListViewItem(d->listView, model, model, port, "/", 
                          TQDateTime::currentDateTime().toString(TQt::ISODate));
    }
}

void SetupCamera::slotAddedCamera(const TQString& title, const TQString& model,
                                  const TQString& port, const TQString& path)
{
    new TQListViewItem(d->listView, title, model, port, path, 
                      TQDateTime::currentDateTime().toString(TQt::ISODate));
}

void SetupCamera::slotEditedCamera(const TQString& title, const TQString& model,
                                   const TQString& port, const TQString& path)
{
    TQListViewItem *item = d->listView->currentItem();
    if (!item) return;

    item->setText(0, title);
    item->setText(1, model);
    item->setText(2, port);
    item->setText(3, path);
}

void SetupCamera::applySettings()
{
    CameraList* clist = CameraList::instance();

    if (clist) 
    {
        clist->clear();

        TQListViewItemIterator it(d->listView);

        for ( ; it.current(); ++it ) 
        {
            TQListViewItem *item  = it.current();
            TQDateTime lastAccess = TQDateTime::currentDateTime();

            if (!item->text(4).isEmpty())
                lastAccess = TQDateTime::fromString(item->text(4), TQt::ISODate);
                            
            CameraType *ctype = new CameraType(item->text(0), item->text(1), item->text(2), 
                                               item->text(3), lastAccess);
            clist->insert(ctype);
        }

        clist->save();
    }
}

}  // namespace Digikam