summaryrefslogtreecommitdiffstats
path: root/digikam/utilities/cameragui/umscamera.cpp
blob: 01e6d982c515a352222222157334459e03c4341b (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2004-12-21
 * Description : USB Mass Storage camera interface
 *
 * Copyright (C) 2004-2005 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Copyright (C) 2005-2008 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.
 *
 * ============================================================ */

// C Ansi includes.

extern "C"
{
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
}

// TQt includes.

#include <tqdir.h>
#include <tqfileinfo.h>
#include <tqfile.h>
#include <tqstringlist.h>
#include <tqdeepcopy.h>
#include <tqwmatrix.h>

// KDE includes.

#include <klocale.h>
#include <kfilemetainfo.h>

// LibKDcraw includes.

#include <libkdcraw/kdcraw.h>

// Local includes.

#include "ddebug.h"
#include "dimg.h"
#include "dmetadata.h"
#include "umscamera.h"

namespace Digikam
{

UMSCamera::UMSCamera(const TQString& title, const TQString& model, const TQString& port, const TQString& path)
         : DKCamera(title, model, port, path)
{
    m_cancel = false;
}

UMSCamera::~UMSCamera()
{
}

bool UMSCamera::doConnect()
{
    return true;
}

void UMSCamera::cancel()
{
    // set the cancel flag
    m_cancel = true;
}

void UMSCamera::getAllFolders(const TQString& folder, TQStringList& subFolderList)
{
    m_cancel = false;
    subFolderList.clear();
    subFolderList.append(folder);
    listFolders(folder, subFolderList);
}

bool UMSCamera::getItemsInfoList(const TQString& folder, GPItemInfoList& infoList, bool getImageDimensions)
{
    m_cancel = false;
    infoList.clear();

    TQDir dir(folder);
    dir.setFilter(TQDir::Files);

    const TQFileInfoList *list = dir.entryInfoList();
    if (!list)
        return false;

    TQFileInfoListIterator it(*list);
    TQFileInfo *fi;
    TQFileInfo thmlo, thmup;
    DMetadata meta;

    while ((fi = it.current()) != 0 && !m_cancel)
    {
        ++it;

        TQString mime = mimeType(fi->extension(false).lower());

        if (!mime.isEmpty())
        {
            TQSize      dims;
            TQDateTime  dt;
            GPItemInfo info;
            thmlo.setFile(folder + TQString("/") + fi->baseName() + TQString(".thm"));
            thmup.setFile(folder + TQString("/") + fi->baseName() + TQString(".THM"));

            if (thmlo.exists())
            {
                // Try thumbnail sidecar files with lowercase extension.
                meta.load(thmlo.filePath());
                dt   = meta.getImageDateTime();
                dims = meta.getImageDimensions();
            }
            else if (thmup.exists())
            {
                // Try thumbnail sidecar files with uppercase extension.
                meta.load(thmup.filePath());
                dt   = meta.getImageDateTime();
                dims = meta.getImageDimensions();
            }
            else if (mime == TQString("image/x-raw"))
            {
                // If no thumbnail sidecar file available , try to load image metadata with Raw files.
                meta.load(fi->filePath());
                dt   = meta.getImageDateTime();
                dims = meta.getImageDimensions();
            }
            else
            {
                meta.load(fi->filePath());
                dt   = meta.getImageDateTime();
                dims = meta.getImageDimensions();

                if (dims.isNull())
                {
                    // In all others case, try KFileMetaInfo.
                    KFileMetaInfo kmeta(fi->filePath());
                    if (kmeta.isValid())
                    {
                        if (kmeta.containsGroup("Jpeg EXIF Data"))
                            dims = kmeta.group("Jpeg EXIF Data").item("Dimensions").value().toSize();
                        else if (kmeta.containsGroup("General"))
                            dims = kmeta.group("General").item("Dimensions").value().toSize();
                        else if (kmeta.containsGroup("Technical"))
                            dims = kmeta.group("Technical").item("Dimensions").value().toSize();
                    }
                }
            }

            if (dt.isNull())
            {
                // If date is not found in metadata, use file time stamp
                dt = fi->created();
            }

            info.name             = fi->fileName();
            info.folder           = !folder.endsWith("/") ? folder + TQString("/") : folder;
            info.mime             = mime;
            info.mtime            = dt.toTime_t();
            info.size             = fi->size();
            info.width            = getImageDimensions ? dims.width()  : -1;
            info.height           = getImageDimensions ? dims.height() : -1;
            info.downloaded       = GPItemInfo::DownloadUnknow;
            info.readPermissions  = fi->isReadable();
            info.writePermissions = fi->isWritable();

            infoList.append(info);
        }
    }

    return true;
}

bool UMSCamera::getThumbnail(const TQString& folder, const TQString& itemName, TQImage& thumbnail)
{
    m_cancel = false;

    // JPEG files: try to get thumbnail from Exif data.

    DMetadata metadata(TQFile::encodeName(folder + TQString("/") + itemName));
    thumbnail = metadata.getExifThumbnail(true);
    if (!thumbnail.isNull())
        return true;

    // RAW files : try to extract embedded thumbnail using dcraw

    KDcrawIface::KDcraw::loadDcrawPreview(thumbnail, TQString(folder + TQString("/") + itemName));
    if (!thumbnail.isNull())
        return true;

    // THM files: try to get thumbnail from '.thm' files if we didn't manage to get 
    // thumbnail from Exif. Any cameras provides *.thm files like JPEG files with RAW/video files. 
    // Using this way speed up thumbnailization and limit data transfered between camera and computer.
    // NOTE: the thumbnail extracted with this method can provide a poor quality preview.

    TQFileInfo fi(folder + TQString("/") + itemName);

    if (thumbnail.load(folder + TQString("/") + fi.baseName() + TQString(".thm")))        // Lowercase
    {
        if (!thumbnail.isNull())
           return true;
    }
    else if (thumbnail.load(folder + TQString("/") + fi.baseName() + TQString(".THM")))   // Uppercase
    {
        if (!thumbnail.isNull())
           return true;
    }

    // Finaly, we trying to get thumbnail using DImg API (slow).

    DImg dimgThumb(TQCString(TQFile::encodeName(folder + TQString("/") + itemName)));

    if (!dimgThumb.isNull())
    {
        thumbnail = dimgThumb.copyTQImage();
        return true;
    }

    return false;
}

bool UMSCamera::getExif(const TQString&, const TQString&, char **, int&)
{
    // not necessary to implement this. read it directly from the file
    // (done in camera controller)
    DWarning() << "exif implemented yet in camera controller" << endl;
    return false;
}

bool UMSCamera::downloadItem(const TQString& folder, const TQString& itemName, const TQString& saveFile)
{
    m_cancel     = false;
    TQString src  = folder + TQString("/") + itemName;
    TQString dest = saveFile;

    TQFile sFile(src);
    TQFile dFile(dest);

    if ( !sFile.open(IO_ReadOnly) )
    {
        DWarning() << "Failed to open source file for reading: "
                   << src << endl;
        return false;
    }

    if ( !dFile.open(IO_WriteOnly) )
    {
        sFile.close();
        DWarning() << "Failed to open dest file for writing: "
                    << dest << endl;
        return false;
    }

    const int MAX_IPC_SIZE = (1024*32);
    char buffer[MAX_IPC_SIZE];

    TQ_LONG len;
    while ((len = sFile.readBlock(buffer, MAX_IPC_SIZE)) != 0 && !m_cancel)
    {
        if (len == -1 || dFile.writeBlock(buffer, (TQ_ULONG)len) != len)
        {
            sFile.close();
            dFile.close();
            return false;
        }
    }

    sFile.close();
    dFile.close();

    // set the file modification time of the downloaded file to that
    // of the original file
    struct stat st;
    ::stat(TQFile::encodeName(src), &st);

    struct utimbuf ut;
    ut.modtime = st.st_mtime;
    ut.actime  = st.st_atime;

    ::utime(TQFile::encodeName(dest), &ut);

    return true;
}

bool UMSCamera::setLockItem(const TQString& folder, const TQString& itemName, bool lock)
{
    TQString src = folder + TQString("/") + itemName;

    if (lock)
    {
        // Lock the file to set read only flag
        if (::chmod(TQFile::encodeName(src), S_IREAD) == -1)
            return false; 
    }
    else
    {
        // Unlock the file to set read/write flag
        if (::chmod(TQFile::encodeName(src), S_IREAD | S_IWRITE) == -1)
            return false; 
    }

    return true;
}

bool UMSCamera::deleteItem(const TQString& folder, const TQString& itemName)
{
    m_cancel = false;

    // Any camera provide THM (thumbnail) file with real image. We need to remove it also.

    TQFileInfo fi(folder + TQString("/") + itemName);

    TQFileInfo thmLo(folder + TQString("/") + fi.baseName() + ".thm");          // Lowercase

    if (thmLo.exists())
        ::unlink(TQFile::encodeName(thmLo.filePath()));

    TQFileInfo thmUp(folder + TQString("/") + fi.baseName() + ".THM");          // Uppercase

    if (thmUp.exists())
        ::unlink(TQFile::encodeName(thmUp.filePath()));

    // Remove the real image.
    return (::unlink(TQFile::encodeName(folder + TQString("/") + itemName)) == 0);
}

bool UMSCamera::uploadItem(const TQString& folder, const TQString& itemName, const TQString& localFile,
                           GPItemInfo& info, bool getImageDimensions)
{
    m_cancel     = false;
    TQString dest = folder + TQString("/") + itemName;
    TQString src  = localFile;

    TQFile sFile(src);
    TQFile dFile(dest);

    if ( !sFile.open(IO_ReadOnly) )
    {
        DWarning() << "Failed to open source file for reading: "
                    << src << endl;
        return false;
    }

    if ( !dFile.open(IO_WriteOnly) )
    {
        sFile.close();
        DWarning() << "Failed to open dest file for writing: "
                    << dest << endl;
        return false;
    }

    const int MAX_IPC_SIZE = (1024*32);
    char buffer[MAX_IPC_SIZE];

    TQ_LONG len;
    while ((len = sFile.readBlock(buffer, MAX_IPC_SIZE)) != 0 && !m_cancel)
    {
        if (len == -1 || dFile.writeBlock(buffer, (TQ_ULONG)len) == -1)
        {
            sFile.close();
            dFile.close();
            return false;
        }
    }

    sFile.close();
    dFile.close();

    // set the file modification time of the uploaded file to that
    // of the original file
    struct stat st;
    ::stat(TQFile::encodeName(src), &st);

    struct utimbuf ut;
    ut.modtime = st.st_mtime;
    ut.actime  = st.st_atime;

    ::utime(TQFile::encodeName(dest), &ut);

    // Get new camera item information.

    DMetadata meta;
    TQFileInfo fi(dest);
    TQString   mime = mimeType(fi.extension(false).lower());

    if (!mime.isEmpty())
    {
        TQSize     dims;
        TQDateTime dt;

        if (mime == TQString("image/x-raw"))
        {
            // Try to load image metadata with Raw files.
            meta.load(fi.filePath());
            dt   = meta.getImageDateTime();
            dims = meta.getImageDimensions();
        }
        else
        {
            meta.load(fi.filePath());
            dt   = meta.getImageDateTime();
            dims = meta.getImageDimensions();

            if (dims.isNull())
            {
                // In all others case, try KFileMetaInfo.
                KFileMetaInfo kmeta(fi.filePath());
                if (kmeta.isValid())
                {
                    if (kmeta.containsGroup("Jpeg EXIF Data"))
                        dims = kmeta.group("Jpeg EXIF Data").item("Dimensions").value().toSize();
                    else if (kmeta.containsGroup("General"))
                        dims = kmeta.group("General").item("Dimensions").value().toSize();
                    else if (kmeta.containsGroup("Technical"))
                        dims = kmeta.group("Technical").item("Dimensions").value().toSize();
                }
            }
        }

        if (dt.isNull())
        {
            // If date is not found in metadata, use file time stamp
            dt = fi.created();
        }

        info.name             = fi.fileName();
        info.folder           = !folder.endsWith("/") ? folder + TQString("/") : folder;
        info.mime             = mime;
        info.mtime            = dt.toTime_t();
        info.size             = fi.size();
        info.width            = getImageDimensions ? dims.width()  : -1;
        info.height           = getImageDimensions ? dims.height() : -1;
        info.downloaded       = GPItemInfo::DownloadUnknow;
        info.readPermissions  = fi.isReadable();
        info.writePermissions = fi.isWritable();
    }

    return true;
}

void UMSCamera::listFolders(const TQString& folder, TQStringList& subFolderList)
{
    if (m_cancel)
        return;

    TQDir dir(folder);
    dir.setFilter(TQDir::Dirs|TQDir::Executable);

    const TQFileInfoList *list = dir.entryInfoList();
    if (!list)
        return;

    TQFileInfoListIterator it( *list );
    TQFileInfo *fi;

    while ((fi = it.current()) != 0 && !m_cancel)
    {
        ++it;

        if (fi->fileName() == "." || fi->fileName() == "..")
            continue;

        TQString subfolder = folder + TQString(folder.endsWith("/") ? "" : "/") + fi->fileName();
        subFolderList.append(subfolder);
        listFolders(subfolder, subFolderList);
    }
}

bool UMSCamera::cameraSummary(TQString& summary)
{
    summary = TQString(i18n("<b>Mounted Camera</b> driver for USB/IEEE1394 mass storage cameras and "
                           "Flash disk card readers.<br><br>"));

    summary.append(i18n("Title: %1<br>"
                        "Model: %2<br>"
                        "Port: %3<br>"
                        "Path: %4<br>")
                        .arg(title())
                        .arg(model())
                        .arg(port())
                        .arg(path()));
    return true;
}

bool UMSCamera::cameraManual(TQString& manual)
{
    manual = TQString(i18n("For more information about the <b>Mounted Camera</b> driver, "
                          "please read <b>Supported Digital Still "
                          "Cameras</b> section in the digiKam manual."));
    return true;
}

bool UMSCamera::cameraAbout(TQString& about)
{
    about = TQString(i18n("The <b>Mounted Camera</b> driver is a simple interface to a camera disk "
                         "mounted locally on your system.<br><br>"
                         "It doesn't use libgphoto2 drivers.<br><br>"
                         "To report any problems with this driver, please contact the digiKam team at:<br><br>"
                         "http://www.digikam.org/?q=contact"));
    return true;
}

}  // namespace Digikam