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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
|
/* ============================================================
*
* This file is a part of digiKam project
* http://www.digikam.org
*
* Date : 2005-04-21
* Description : a kio-slave to process file operations on
* digiKam albums.
*
* Copyright (C) 2005 by Renchi Raju <[email protected]>
*
* Lots of the file io code is copied from KDE file tdeioslave.
* Copyright for the KDE file tdeioslave follows:
* Copyright (C) 2000-2002 Stephan Kulow <[email protected]>
* Copyright (C) 2000-2002 David Faure <[email protected]>
* Copyright (C) 2000-2002 Waldo Bastian <[email protected]>
*
* 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.
*
* ============================================================ */
#define MAX_IPC_SIZE (1024*32)
// C Ansi includes.
extern "C"
{
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <utime.h>
}
// C++ includes.
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cerrno>
// TQt includes.
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqdatastream.h>
#include <tqregexp.h>
#include <tqdir.h>
// KDE includes.
#include <tdeglobal.h>
#include <tdelocale.h>
#include <kinstance.h>
#include <tdefilemetainfo.h>
#include <kmimetype.h>
#include <kdebug.h>
#include <tdeio/global.h>
#include <tdeio/ioslave_defaults.h>
#include <klargefile.h>
#include <tdeversion.h>
// LibKDcraw includes.
#include <libkdcraw/version.h>
#include <libkdcraw/kdcraw.h>
#if KDCRAW_VERSION < 0x000106
#include <libkdcraw/dcrawbinary.h>
#endif
// Local includes.
#include "dmetadata.h"
#include "sqlitedb.h"
#include "digikam_export.h"
#include "digikamalbums.h"
tdeio_digikamalbums::tdeio_digikamalbums(const TQCString &pool_socket,
const TQCString &app_socket)
: SlaveBase("tdeio_digikamalbums", pool_socket, app_socket)
{
}
tdeio_digikamalbums::~tdeio_digikamalbums()
{
}
static TQValueList<TQRegExp> makeFilterList( const TQString &filter )
{
TQValueList<TQRegExp> regExps;
if ( filter.isEmpty() )
return regExps;
TQChar sep( ';' );
int i = filter.find( sep, 0 );
if ( i == -1 && filter.find( ' ', 0 ) != -1 )
sep = TQChar( ' ' );
TQStringList list = TQStringList::split( sep, filter );
TQStringList::Iterator it = list.begin();
while ( it != list.end() ) {
regExps << TQRegExp( (*it).stripWhiteSpace(), false, true );
++it;
}
return regExps;
}
static bool matchFilterList( const TQValueList<TQRegExp>& filters,
const TQString &fileName )
{
TQValueList<TQRegExp>::ConstIterator rit = filters.begin();
while ( rit != filters.end() ) {
if ( (*rit).exactMatch(fileName) )
return true;
++rit;
}
return false;
}
void tdeio_digikamalbums::special(const TQByteArray& data)
{
bool folders = (metaData("folders") == "yes");
TQString libraryPath;
KURL kurl;
TQString url;
TQString urlWithTrailingSlash;
TQString filter;
int getDimensions;
int scan = 0;
int recurseAlbums;
int recurseTags;
TQDataStream ds(data, IO_ReadOnly);
ds >> libraryPath;
ds >> kurl;
ds >> filter;
ds >> getDimensions;
ds >> recurseAlbums;
ds >> recurseTags;
if (!ds.atEnd())
ds >> scan;
libraryPath = TQDir::cleanDirPath(libraryPath);
if (m_libraryPath != libraryPath)
{
m_libraryPath = libraryPath;
m_sqlDB.closeDB();
m_sqlDB.openDB(libraryPath);
}
url = kurl.path();
if (scan)
{
scanAlbum(url);
finished();
return;
}
TQValueList<TQRegExp> regex = makeFilterList(filter);
TQByteArray ba;
if (folders) // Special mode to stats all album items
{
TQMap<int, int> albumsStatMap;
TQStringList values, allAbumIDs;
int albumID;
// initialize allAbumIDs with all existing albums from db to prevent
// wrong album image counters
m_sqlDB.execSql(TQString("SELECT id from Albums"), &allAbumIDs);
for ( TQStringList::iterator it = allAbumIDs.begin(); it != allAbumIDs.end(); ++it)
{
albumID = (*it).toInt();
albumsStatMap.insert(albumID, 0);
}
// now we can count the images assigned to albums
m_sqlDB.execSql(TQString("SELECT dirid, Images.name FROM Images "
"WHERE Images.dirid IN (SELECT DISTINCT id FROM Albums)"), &values);
for ( TQStringList::iterator it = values.begin(); it != values.end(); )
{
albumID = (*it).toInt();
++it;
if ( matchFilterList( regex, *it ) )
{
TQMap<int, int>::iterator it2 = albumsStatMap.find(albumID);
if ( it2 == albumsStatMap.end() )
albumsStatMap.insert(albumID, 1);
else
albumsStatMap.replace(albumID, it2.data() + 1);
}
++it;
}
TQDataStream os(ba, IO_WriteOnly);
os << albumsStatMap;
}
else
{
TQStringList albumvalues;
if (recurseAlbums)
{
// Search for albums and sub-albums:
// For this, get the path with a trailing "/".
// Otherwise albums on the same level like "Paris", "Paris 2006",
// would be found in addition to "Paris/*".
urlWithTrailingSlash = kurl.path(1);
m_sqlDB.execSql(TQString("SELECT DISTINCT id, url FROM Albums WHERE url='%1' OR url LIKE '%2\%';")
.arg(escapeString(url)).arg(escapeString(urlWithTrailingSlash)), &albumvalues);
}
else
{
// Search for albums
m_sqlDB.execSql(TQString("SELECT DISTINCT id, url FROM Albums WHERE url='%1';")
.arg(escapeString(url)), &albumvalues);
}
TQDataStream* os = new TQDataStream(ba, IO_WriteOnly);
TQString base;
TQ_LLONG id;
TQString name;
TQString date;
TQSize dims;
struct stat stbuf;
TQStringList values;
TQString albumurl;
int albumid;
// Loop over all albums:
int count = 0 ;
for (TQStringList::iterator albumit = albumvalues.begin(); albumit != albumvalues.end();)
{
albumid = (*albumit).toLongLong();
++albumit;
albumurl = *albumit;
++albumit;
base = libraryPath + albumurl + '/';
values.clear();
m_sqlDB.execSql(TQString("SELECT id, name, datetime FROM Images "
"WHERE dirid = %1;")
.arg(albumid), &values);
// Loop over all images in each album (specified by its albumid).
for (TQStringList::iterator it = values.begin(); it != values.end();)
{
id = (*it).toLongLong();
++it;
name = *it;
++it;
date = *it;
++it;
if (!matchFilterList(regex, name))
continue;
if (::stat(TQFile::encodeName(base + name), &stbuf) != 0)
continue;
dims = TQSize();
if (getDimensions)
{
TQFileInfo fileInfo(base + name);
#if KDCRAW_VERSION < 0x000106
TQString rawFilesExt(KDcrawIface::DcrawBinary::instance()->rawFiles());
#else
TQString rawFilesExt(KDcrawIface::KDcraw::rawFiles());
#endif
TQString ext = fileInfo.extension(false).upper();
if (!ext.isEmpty() && rawFilesExt.upper().contains(ext))
{
Digikam::DMetadata metaData(base + name);
dims = metaData.getImageDimensions();
}
else
{
KFileMetaInfo metaInfo(base + name);
if (metaInfo.isValid())
{
if (metaInfo.containsGroup("Jpeg EXIF Data"))
{
dims = metaInfo.group("Jpeg EXIF Data").
item("Dimensions").value().toSize();
}
else if (metaInfo.containsGroup("General"))
{
dims = metaInfo.group("General").
item("Dimensions").value().toSize();
}
else if (metaInfo.containsGroup("Technical"))
{
dims = metaInfo.group("Technical").
item("Dimensions").value().toSize();
}
}
}
}
*os << id;
*os << albumid;
*os << name;
*os << date;
*os << static_cast<size_t>(stbuf.st_size);
*os << dims;
count++;
// Send images in batches of 200.
if (count > 200)
{
delete os;
os = 0;
SlaveBase::data(ba);
ba.resize(0);
count = 0;
os = new TQDataStream(ba, IO_WriteOnly);
}
}
count++;
}
}
SlaveBase::data(ba);
finished();
}
static int write_all(int fd, const char *buf, size_t len)
{
while (len > 0)
{
ssize_t written = write(fd, buf, len);
if (written < 0)
{
if (errno == EINTR)
continue;
return -1;
}
buf += written;
len -= written;
}
return 0;
}
void tdeio_digikamalbums::get( const KURL& url )
{
// Code duplication from file:// ioslave
kdDebug() << k_funcinfo << " : " << url << endl;
// get the libraryPath
TQString libraryPath = url.user();
if (libraryPath.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, "Album Library Path not supplied to tdeioslave");
return;
}
// no need to open the db. we don't need to read/write to it
TQCString path(TQFile::encodeName(libraryPath + url.path()));
KDE_struct_stat buff;
if ( KDE_stat( path.data(), &buff ) == -1 )
{
if ( errno == EACCES )
error( TDEIO::ERR_ACCESS_DENIED, url.url() );
else
error( TDEIO::ERR_DOES_NOT_EXIST, url.url() );
return;
}
if ( S_ISDIR( buff.st_mode ) )
{
error( TDEIO::ERR_IS_DIRECTORY, url.url() );
return;
}
if ( !S_ISREG( buff.st_mode ) )
{
error( TDEIO::ERR_CANNOT_OPEN_FOR_READING, url.url() );
return;
}
int fd = KDE_open( path.data(), O_RDONLY);
if ( fd < 0 )
{
error( TDEIO::ERR_CANNOT_OPEN_FOR_READING, url.url() );
return;
}
// Determine the mimetype of the file to be retrieved, and emit it.
// This is mandatory in all slaves (for KRun/BrowserRun to work).
KMimeType::Ptr mt = KMimeType::findByURL( libraryPath + url.path(), buff.st_mode,
true);
emit mimeType( mt->name() );
totalSize( buff.st_size );
char buffer[ MAX_IPC_SIZE ];
TQByteArray array;
TDEIO::filesize_t processed_size = 0;
while (1)
{
int n = ::read( fd, buffer, MAX_IPC_SIZE );
if (n == -1)
{
if (errno == EINTR)
continue;
error( TDEIO::ERR_COULD_NOT_READ, url.url());
close(fd);
return;
}
if (n == 0)
break; // Finished
array.setRawData(buffer, n);
data( array );
array.resetRawData(buffer, n);
processed_size += n;
processedSize( processed_size );
}
data( TQByteArray() );
close( fd );
processedSize( buff.st_size );
finished();
}
void tdeio_digikamalbums::put(const KURL& url, int permissions, bool overwrite, bool /*resume*/)
{
// Code duplication from file:// ioslave
kdDebug() << k_funcinfo << " : " << url.url() << endl;
// get the libraryPath
TQString libraryPath = url.user();
if (libraryPath.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, "Album Library Path not supplied to tdeioslave");
return;
}
// open the db if needed
if (m_libraryPath != libraryPath)
{
m_libraryPath = libraryPath;
m_sqlDB.closeDB();
m_sqlDB.openDB(m_libraryPath);
}
// build the album list
buildAlbumList();
// get the parent album
AlbumInfo album = findAlbum(url.directory());
if (album.id == -1)
{
error(TDEIO::ERR_UNKNOWN, i18n("Source album %1 not found in database")
.arg(url.directory()));
return;
}
TQString dest = libraryPath + url.path();
TQCString _dest( TQFile::encodeName(dest));
// check if the original file exists and we are not allowed to overwrite it
KDE_struct_stat buff;
bool origExists = (KDE_lstat( _dest.data(), &buff ) != -1);
if ( origExists && !overwrite)
{
if (S_ISDIR(buff.st_mode))
error( TDEIO::ERR_DIR_ALREADY_EXIST, url.url() );
else
error( TDEIO::ERR_FILE_ALREADY_EXIST, url.url() );
return;
}
// get the permissions we are supposed to set
mode_t initialPerms;
if (permissions != -1)
initialPerms = permissions | S_IWUSR | S_IRUSR;
else
initialPerms = 0666;
// open the destination file
int fd = KDE_open(_dest.data(), O_CREAT | O_TRUNC | O_WRONLY, initialPerms);
if ( fd < 0 )
{
kdWarning() << "####################### COULD NOT OPEN " << dest << endl;
if ( errno == EACCES )
error( TDEIO::ERR_WRITE_ACCESS_DENIED, url.url() );
else
error( TDEIO::ERR_CANNOT_OPEN_FOR_WRITING, url.url() );
return;
}
int result;
// Loop until we get 0 (end of data)
do
{
TQByteArray buffer;
dataReq();
result = readData( buffer );
if (result >= 0)
{
if (write_all( fd, buffer.data(), buffer.size()))
{
if ( errno == ENOSPC ) // disk full
{
error( TDEIO::ERR_DISK_FULL, url.url());
result = -1;
}
else
{
kdWarning() << "Couldn't write. Error:" << strerror(errno) << endl;
error( TDEIO::ERR_COULD_NOT_WRITE, url.url());
result = -1;
}
}
}
}
while ( result > 0 );
// An error occurred deal with it.
if (result < 0)
{
kdDebug() << "Error during 'put'. Aborting." << endl;
close(fd);
remove(_dest);
return;
}
// close the file
if ( close(fd) )
{
kdWarning() << "Error when closing file descriptor:" << strerror(errno) << endl;
error( TDEIO::ERR_COULD_NOT_WRITE, url.url());
return;
}
// set final permissions
if ( permissions != -1 )
{
if (::chmod(_dest.data(), permissions) != 0)
{
// couldn't chmod. Eat the error if the filesystem apparently doesn't support it.
if ( TDEIO::testFileSystemFlag( _dest, TDEIO::SupportsChmod ) )
warning( i18n( "Could not change permissions for\n%1" ).arg( url.url() ) );
}
}
// set modification time
const TQString mtimeStr = metaData( "modified" );
if ( !mtimeStr.isEmpty() ) {
TQDateTime dt = TQDateTime::fromString( mtimeStr, Qt::ISODate );
if ( dt.isValid() ) {
KDE_struct_stat dest_statbuf;
if (KDE_stat( _dest.data(), &dest_statbuf ) == 0) {
struct utimbuf utbuf;
utbuf.actime = dest_statbuf.st_atime; // access time, unchanged
utbuf.modtime = dt.toTime_t(); // modification time
kdDebug() << k_funcinfo << "setting modtime to " << utbuf.modtime << endl;
utime( _dest.data(), &utbuf );
}
}
}
// First check if the file is already in database
if (!findImage(album.id, url.fileName()))
{
// Now insert the file into the database
addImage(album.id, m_libraryPath + url.path());
}
// We have done our job => finish
finished();
}
void tdeio_digikamalbums::copy( const KURL &src, const KURL &dst, int mode, bool overwrite )
{
// Code duplication from file:// ioslave?
kdDebug() << k_funcinfo << "Src: " << src.path() << ", Dst: " << dst.path() << endl;
// get the album library path
TQString libraryPath = src.user();
if (libraryPath.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, "Album Library Path not supplied to tdeioslave");
return;
}
// check that the src and dst album library paths match
TQString dstLibraryPath = dst.user();
if (libraryPath != dstLibraryPath)
{
error(TDEIO::ERR_UNKNOWN,
TQString("Source and Destination have different Album Library Paths. ") +
TQString("Src: ") + src.user() +
TQString(", Dest: ") + dst.user());
return;
}
// open the db if needed
if (m_libraryPath != libraryPath)
{
m_libraryPath = libraryPath;
m_sqlDB.closeDB();
m_sqlDB.openDB(m_libraryPath);
}
// build the album list
buildAlbumList();
// find the src parent album
AlbumInfo srcAlbum = findAlbum(src.directory());
if (srcAlbum.id == -1)
{
error(TDEIO::ERR_UNKNOWN, TQString("Source album %1 not found in database")
.arg(src.directory()));
return;
}
// find the dst parent album
AlbumInfo dstAlbum = findAlbum(dst.directory());
if (dstAlbum.id == -1)
{
error(TDEIO::ERR_UNKNOWN, TQString("Destination album %1 not found in database")
.arg(dst.directory()));
return;
}
// if the filename is .digikam_properties, we have been asked to copy the
// metadata of the src album to the dst album
if (src.fileName() == ".digikam_properties")
{
// no duplication in AlbumDB?
// copy metadata of album to destination album
m_sqlDB.execSql( TQString("UPDATE Albums SET date='%1', caption='%2', "
"collection='%3', icon=%4 ")
.arg(srcAlbum.date.toString(Qt::ISODate),
escapeString(srcAlbum.caption),
escapeString(srcAlbum.collection),
TQString::number(srcAlbum.icon)) +
TQString( " WHERE id=%1" )
.arg(dstAlbum.id) );
finished();
return;
}
TQCString _src( TQFile::encodeName(libraryPath + src.path()));
TQCString _dst( TQFile::encodeName(libraryPath + dst.path()));
// stat the src file
KDE_struct_stat buff_src;
if ( KDE_stat( _src.data(), &buff_src ) == -1 )
{
if ( errno == EACCES )
error( TDEIO::ERR_ACCESS_DENIED, src.url() );
else
error( TDEIO::ERR_DOES_NOT_EXIST, src.url() );
return;
}
// bail out if its a directory
if ( S_ISDIR( buff_src.st_mode ) )
{
error( TDEIO::ERR_IS_DIRECTORY, src.url() );
return;
}
// bail out if its a socket or fifo
if ( S_ISFIFO( buff_src.st_mode ) || S_ISSOCK ( buff_src.st_mode ) )
{
error( TDEIO::ERR_CANNOT_OPEN_FOR_READING, src.url() );
return;
}
// stat the dst file
KDE_struct_stat buff_dest;
bool dest_exists = ( KDE_lstat( _dst.data(), &buff_dest ) != -1 );
if ( dest_exists )
{
// bail out if its a directory
if (S_ISDIR(buff_dest.st_mode))
{
error( TDEIO::ERR_DIR_ALREADY_EXIST, dst.url() );
return;
}
// if !overwrite bail out
if (!overwrite)
{
error( TDEIO::ERR_FILE_ALREADY_EXIST, dst.url() );
return;
}
// If the destination is a symlink and overwrite is true,
// remove the symlink first to prevent the scenario where
// the symlink actually points to current source!
if (overwrite && S_ISLNK(buff_dest.st_mode))
{
remove( _dst.data() );
}
}
// now open the src file
int src_fd = KDE_open( _src.data(), O_RDONLY);
if ( src_fd < 0 )
{
error( TDEIO::ERR_CANNOT_OPEN_FOR_READING, src.path() );
return;
}
// get the permissions we are supposed to set
mode_t initialMode;
if (mode != -1)
initialMode = mode | S_IWUSR;
else
initialMode = 0666;
// open the destination file
int dest_fd = KDE_open(_dst.data(), O_CREAT | O_TRUNC | O_WRONLY, initialMode);
if ( dest_fd < 0 )
{
kdDebug() << "###### COULD NOT WRITE " << dst.url() << endl;
if ( errno == EACCES )
{
error( TDEIO::ERR_WRITE_ACCESS_DENIED, dst.url() );
}
else
{
error( TDEIO::ERR_CANNOT_OPEN_FOR_WRITING, dst.url() );
}
close(src_fd);
return;
}
// emit the total size for copying
totalSize( buff_src.st_size );
TDEIO::filesize_t processed_size = 0;
char buffer[ MAX_IPC_SIZE ];
int n;
while (1)
{
// read in chunks of MAX_IPC_SIZE
n = ::read( src_fd, buffer, MAX_IPC_SIZE );
if (n == -1)
{
if (errno == EINTR)
continue;
error( TDEIO::ERR_COULD_NOT_READ, src.path());
close(src_fd);
close(dest_fd);
return;
}
// Finished ?
if (n == 0)
break;
// write to the destination file
if (write_all( dest_fd, buffer, n))
{
close(src_fd);
close(dest_fd);
if ( errno == ENOSPC ) // disk full
{
error( TDEIO::ERR_DISK_FULL, dst.url());
remove( _dst.data() );
}
else
{
kdWarning() << "Couldn't write[2]. Error:" << strerror(errno) << endl;
error( TDEIO::ERR_COULD_NOT_WRITE, dst.url());
}
return;
}
processedSize( processed_size );
}
close( src_fd );
if (close( dest_fd))
{
kdWarning() << "Error when closing file descriptor[2]:" << strerror(errno) << endl;
error( TDEIO::ERR_COULD_NOT_WRITE, dst.url());
return;
}
// set final permissions
if ( mode != -1 )
{
if (::chmod(_dst.data(), mode) != 0)
{
// Eat the error if the filesystem apparently doesn't support chmod.
if ( TDEIO::testFileSystemFlag( _dst, TDEIO::SupportsChmod ) )
warning( i18n( "Could not change permissions for\n%1" ).arg( dst.url() ) );
}
}
// copy access and modification time
struct utimbuf ut;
ut.actime = buff_src.st_atime;
ut.modtime = buff_src.st_mtime;
if ( ::utime( _dst.data(), &ut ) != 0 )
{
kdWarning() << TQString::fromLatin1("Couldn't preserve access and modification time for\n%1")
.arg( dst.url() ) << endl;
}
// now copy the metadata over
copyImage(srcAlbum.id, src.fileName(), dstAlbum.id, dst.fileName());
processedSize( buff_src.st_size );
finished();
}
void tdeio_digikamalbums::rename( const KURL& src, const KURL& dst, bool overwrite )
{
// Code duplication from file:// ioslave?
kdDebug() << k_funcinfo << "Src: " << src << ", Dst: " << dst << endl;
// if the filename is .digikam_properties fake that we renamed it
if (src.fileName() == ".digikam_properties")
{
finished();
return;
}
TQString libraryPath = src.user();
if (libraryPath.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, "Album Library Path not supplied to tdeioslave");
return;
}
TQString dstLibraryPath = dst.user();
if (libraryPath != dstLibraryPath)
{
error(TDEIO::ERR_UNKNOWN,
i18n("Source and Destination have different Album Library Paths.\n"
"Source: %1\n"
"Destination: %2")
.arg(src.user())
.arg(dst.user()));
return;
}
// open album db if needed
if (m_libraryPath != libraryPath)
{
m_libraryPath = libraryPath;
m_sqlDB.closeDB();
m_sqlDB.openDB(m_libraryPath);
}
TQCString csrc( TQFile::encodeName(libraryPath + src.path()));
TQCString cdst( TQFile::encodeName(libraryPath + dst.path()));
// stat the source file/folder
KDE_struct_stat buff_src;
if ( KDE_stat( csrc.data(), &buff_src ) == -1 )
{
if ( errno == EACCES )
error( TDEIO::ERR_ACCESS_DENIED, src.url() );
else
error( TDEIO::ERR_DOES_NOT_EXIST, src.url() );
return;
}
// stat the destination file/folder
KDE_struct_stat buff_dest;
bool dest_exists = ( KDE_stat( cdst.data(), &buff_dest ) != -1 );
if ( dest_exists )
{
if (S_ISDIR(buff_dest.st_mode))
{
error( TDEIO::ERR_DIR_ALREADY_EXIST, dst.url() );
return;
}
if (!overwrite)
{
error( TDEIO::ERR_FILE_ALREADY_EXIST, dst.url() );
return;
}
}
// build album list
buildAlbumList();
AlbumInfo srcAlbum, dstAlbum;
// check if we are renaming an album or a image
bool renamingAlbum = S_ISDIR(buff_src.st_mode);
if (renamingAlbum)
{
srcAlbum = findAlbum(src.path());
if (srcAlbum.id == -1)
{
error(TDEIO::ERR_UNKNOWN, i18n("Source album %1 not found in database")
.arg(src.url()));
return;
}
}
else
{
srcAlbum = findAlbum(src.directory());
if (srcAlbum.id == -1)
{
error(TDEIO::ERR_UNKNOWN, i18n("Source album %1 not found in database")
.arg(src.directory()));
return;
}
dstAlbum = findAlbum(dst.directory());
if (dstAlbum.id == -1)
{
error(TDEIO::ERR_UNKNOWN, i18n("Destination album %1 not found in database")
.arg(dst.directory()));
return;
}
}
// actually rename the file/folder
if ( ::rename(csrc.data(), cdst.data()))
{
if (( errno == EACCES ) || (errno == EPERM))
{
TQFileInfo toCheck(libraryPath + src.path());
if (!toCheck.isWritable())
error( TDEIO::ERR_CANNOT_RENAME_ORIGINAL, src.path() );
else
error( TDEIO::ERR_ACCESS_DENIED, dst.path() );
}
else if (errno == EXDEV)
{
error( TDEIO::ERR_UNSUPPORTED_ACTION, i18n("This file/folder is on a different "
"filesystem through symlinks. "
"Moving/Renaming files between "
"them is currently unsupported "));
}
else if (errno == EROFS)
{ // The file is on a read-only filesystem
error( TDEIO::ERR_CANNOT_DELETE, src.url() );
}
else {
error( TDEIO::ERR_CANNOT_RENAME, src.url() );
}
return;
}
// renaming done. now update the database
if (renamingAlbum)
{
renameAlbum(srcAlbum.url, dst.path());
}
else
{
renameImage(srcAlbum.id, src.fileName(),
dstAlbum.id, dst.fileName());
}
finished();
}
void tdeio_digikamalbums::stat( const KURL& url )
{
TQString libraryPath = url.user();
if (libraryPath.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, "Album Library Path not supplied to tdeioslave");
return;
}
TDEIO::UDSEntry entry;
if (!createUDSEntry(libraryPath + url.path(), entry))
{
error(TDEIO::ERR_DOES_NOT_EXIST, url.path(-1));
return;
}
statEntry(entry);
finished();
}
void tdeio_digikamalbums::listDir( const KURL& url )
{
// Code duplication from file:// ioslave?
kdDebug() << k_funcinfo << " : " << url.path() << endl;
TQString libraryPath = url.user();
if (libraryPath.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, "Album Library Path not supplied to tdeioslave");
kdWarning() << "Album Library Path not supplied to tdeioslave" << endl;
return;
}
KDE_struct_stat stbuf;
TQString path = libraryPath + url.path();
if (KDE_stat(TQFile::encodeName(path), &stbuf) != 0)
{
error(TDEIO::ERR_DOES_NOT_EXIST, url.path(-1));
return;
}
TQDir dir(path);
if (!dir.isReadable())
{
error( TDEIO::ERR_CANNOT_ENTER_DIRECTORY, url.path());
return;
}
const TQFileInfoList *list = dir.entryInfoList(TQDir::All|TQDir::Hidden);
TQFileInfoListIterator it( *list );
TQFileInfo *fi;
TDEIO::UDSEntry entry;
createDigikamPropsUDSEntry(entry);
listEntry(entry, false);
while ((fi = it.current()) != 0)
{
if (fi->fileName() != "." && fi->fileName() != ".." || fi->extension(true) == "digikamtempfile.tmp")
{
createUDSEntry(fi->absFilePath(), entry);
listEntry(entry, false);
}
++it;
}
entry.clear();
listEntry(entry, true);
finished();
}
void tdeio_digikamalbums::mkdir( const KURL& url, int permissions )
{
// Code duplication from file:// ioslave?
kdDebug() << k_funcinfo << " : " << url.url() << endl;
TQString libraryPath = url.user();
if (libraryPath.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, "Album Library Path not supplied to tdeioslave");
return;
}
if (m_libraryPath != libraryPath)
{
m_libraryPath = libraryPath;
m_sqlDB.closeDB();
m_sqlDB.openDB(m_libraryPath);
}
TQString path = libraryPath + url.path();
TQCString _path( TQFile::encodeName(path));
KDE_struct_stat buff;
if ( KDE_stat( _path, &buff ) == -1 )
{
if ( ::mkdir( _path.data(), 0777 /*umask will be applied*/ ) != 0 )
{
if ( errno == EACCES )
{
error( TDEIO::ERR_ACCESS_DENIED, path );
return;
}
else if ( errno == ENOSPC )
{
error( TDEIO::ERR_DISK_FULL, path );
return;
}
else
{
error( TDEIO::ERR_COULD_NOT_MKDIR, path );
return;
}
}
else
{
// code similar to AlbumDB::addAlbum
m_sqlDB.execSql( TQString("REPLACE INTO Albums (url, date) "
"VALUES('%1','%2')")
.arg(escapeString(url.path()),
TQDate::currentDate().toString(Qt::ISODate)) );
if ( permissions != -1 )
{
if ( ::chmod( _path.data(), permissions ) == -1 )
error( TDEIO::ERR_CANNOT_CHMOD, path );
else
finished();
}
else
finished();
return;
}
}
if ( S_ISDIR( buff.st_mode ) )
{
error( TDEIO::ERR_DIR_ALREADY_EXIST, path );
return;
}
error( TDEIO::ERR_FILE_ALREADY_EXIST, path );
}
void tdeio_digikamalbums::chmod( const KURL& url, int permissions )
{
// Code duplication from file:// ioslave?
kdDebug() << k_funcinfo << " : " << url.url() << endl;
// get the album library path
TQString libraryPath = url.user();
if (libraryPath.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, "Album Library Path not supplied to tdeioslave");
return;
}
TQCString path( TQFile::encodeName(libraryPath + url.path()));
if ( ::chmod( path.data(), permissions ) == -1 )
error( TDEIO::ERR_CANNOT_CHMOD, url.url() );
else
finished();
}
void tdeio_digikamalbums::del( const KURL& url, bool isfile)
{
// Code duplication from file:// ioslave?
kdDebug() << k_funcinfo << " : " << url.url() << endl;
// get the album library path
TQString libraryPath = url.user();
if (libraryPath.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, "Album Library Path not supplied to tdeioslave");
return;
}
// open the db if needed
if (m_libraryPath != libraryPath)
{
m_libraryPath = libraryPath;
m_sqlDB.closeDB();
m_sqlDB.openDB(m_libraryPath);
}
// build the album list
buildAlbumList();
TQCString path( TQFile::encodeName(libraryPath + url.path()));
if (isfile)
{
kdDebug( ) << "Deleting file "<< url.url() << endl;
// if the filename is .digikam_properties fake that we deleted it
if (url.fileName() == ".digikam_properties")
{
finished();
return;
}
// find the Album to which this file belongs.
AlbumInfo album = findAlbum(url.directory());
if (album.id == -1)
{
error(TDEIO::ERR_UNKNOWN, i18n("Source album %1 not found in database")
.arg(url.directory()));
return;
}
// actually delete the file
if ( unlink( path.data() ) == -1 )
{
if ((errno == EACCES) || (errno == EPERM))
error( TDEIO::ERR_ACCESS_DENIED, url.url());
else if (errno == EISDIR)
error( TDEIO::ERR_IS_DIRECTORY, url.url());
else
error( TDEIO::ERR_CANNOT_DELETE, url.url() );
return;
}
// successful deletion. now remove file entry from the database
delImage(album.id, url.fileName());
}
else
{
kdDebug( ) << "Deleting directory " << url.url() << endl;
// find the corresponding album entry
AlbumInfo album = findAlbum(url.path());
if (album.id == -1)
{
error(TDEIO::ERR_UNKNOWN, i18n("Source album %1 not found in database")
.arg(url.path()));
return;
}
if ( ::rmdir( path.data() ) == -1 )
{
// TODO handle symlink delete
if ((errno == EACCES) || (errno == EPERM))
{
error( TDEIO::ERR_ACCESS_DENIED, url.url());
return;
}
else
{
kdDebug() << "could not rmdir " << perror << endl;
error( TDEIO::ERR_COULD_NOT_RMDIR, url.url() );
return;
}
}
// successful deletion. now remove album entry from the database
delAlbum(album.id);
}
finished();
}
bool tdeio_digikamalbums::createUDSEntry(const TQString& path, TDEIO::UDSEntry& entry)
{
entry.clear();
KDE_struct_stat stbuf;
if (KDE_stat(TQFile::encodeName(path), &stbuf) != 0)
return false;
TDEIO::UDSAtom atom;
atom.m_uds = TDEIO::UDS_FILE_TYPE;
atom.m_long = stbuf.st_mode & S_IFMT;
entry.append( atom );
atom.m_uds = TDEIO::UDS_ACCESS;
atom.m_long = stbuf.st_mode & 07777;
entry.append( atom );
atom.m_uds = TDEIO::UDS_SIZE;
atom.m_long = stbuf.st_size;
entry.append( atom );
atom.m_uds = TDEIO::UDS_MODIFICATION_TIME;
atom.m_long = stbuf.st_mtime;
entry.append( atom );
atom.m_uds = TDEIO::UDS_ACCESS_TIME;
atom.m_long = stbuf.st_atime;
entry.append( atom );
atom.m_uds = TDEIO::UDS_NAME;
atom.m_str = TQFileInfo(path).fileName();
entry.append(atom);
/*
// If we provide the local path, a TDEIO::CopyJob will optimize away
// the use of our custom digikamalbums:/ ioslave, which breaks
// copying the database entry:
// Disabling this as a temporary solution for bug #137282
// This code is intended as a fix for bug #122653.
#if KDE_IS_VERSION(3,4,0)
atom.m_uds = TDEIO::UDS_LOCAL_PATH;
atom.m_str = path;
entry.append(atom);
#endif
*/
return true;
}
void tdeio_digikamalbums::createDigikamPropsUDSEntry(TDEIO::UDSEntry& entry)
{
entry.clear();
TDEIO::UDSAtom atom;
atom.m_uds = TDEIO::UDS_FILE_TYPE;
atom.m_long = S_IFREG;
entry.append( atom );
atom.m_uds = TDEIO::UDS_ACCESS;
atom.m_long = 00666;
entry.append( atom );
atom.m_uds = TDEIO::UDS_SIZE;
atom.m_long = 0;
entry.append( atom );
atom.m_uds = TDEIO::UDS_MODIFICATION_TIME;
atom.m_long = TQDateTime::currentDateTime().toTime_t();
entry.append( atom );
atom.m_uds = TDEIO::UDS_ACCESS_TIME;
atom.m_long = TQDateTime::currentDateTime().toTime_t();
entry.append( atom );
atom.m_uds = TDEIO::UDS_NAME;
atom.m_str = ".digikam_properties";
entry.append(atom);
}
void tdeio_digikamalbums::buildAlbumList()
{
// simplified from AlbumDB::scanAlbums()
m_albumList.clear();
TQStringList values;
m_sqlDB.execSql( TQString("SELECT id, url, date, caption, collection, icon "
"FROM Albums;"), &values );
for (TQStringList::iterator it = values.begin(); it != values.end();)
{
AlbumInfo info;
info.id = (*it).toInt();
++it;
info.url = *it;
++it;
info.date = TQDate::fromString(*it, Qt::ISODate);
++it;
info.caption = *it;
++it;
info.collection = *it;
++it;
info.icon = (*it).toLongLong();
++it;
m_albumList.append(info);
}
}
AlbumInfo tdeio_digikamalbums::findAlbum(const TQString& url, bool addIfNotExists)
{
// similar to AlbumDB::getOrCreateAlbumId
AlbumInfo album;
for (TQValueList<AlbumInfo>::const_iterator it = m_albumList.begin();
it != m_albumList.end(); ++it)
{
if ((*it).url == url)
{
album = *it;
return album;
}
}
album.id = -1;
if (addIfNotExists)
{
TQFileInfo fi(m_libraryPath + url);
if (!fi.exists() || !fi.isDir())
return album;
m_sqlDB.execSql(TQString("INSERT INTO Albums (url, date) "
"VALUES('%1', '%2')")
.arg(escapeString(url),
fi.lastModified().date().toString(Qt::ISODate)));
album.id = m_sqlDB.lastInsertedRow();
album.url = url;
album.date = fi.lastModified().date();
album.icon = 0;
m_albumList.append(album);
}
return album;
}
void tdeio_digikamalbums::delAlbum(int albumID)
{
// code duplication from AlbumDB::deleteAlbum
m_sqlDB.execSql(TQString("DELETE FROM Albums WHERE id='%1'")
.arg(albumID));
}
void tdeio_digikamalbums::renameAlbum(const TQString& oldURL, const TQString& newURL)
{
// similar to AlbumDB::setAlbumURL, but why more extended?
// first update the url of the album which was renamed
m_sqlDB.execSql( TQString("UPDATE Albums SET url='%1' WHERE url='%2'")
.arg(escapeString(newURL),
escapeString(oldURL)));
// now find the list of all subalbums which need to be updated
TQStringList values;
m_sqlDB.execSql( TQString("SELECT url FROM Albums WHERE url LIKE '%1/%';")
.arg(oldURL), &values );
// and update their url
TQString newChildURL;
for (TQStringList::iterator it = values.begin(); it != values.end(); ++it)
{
newChildURL = *it;
newChildURL.replace(oldURL, newURL);
m_sqlDB.execSql(TQString("UPDATE Albums SET url='%1' WHERE url='%2'")
.arg(escapeString(newChildURL),
escapeString(*it)));
}
}
bool tdeio_digikamalbums::findImage(int albumID, const TQString& name) const
{
// no similar method in AlbumDB?
TQStringList values;
m_sqlDB.execSql( TQString("SELECT name FROM Images "
"WHERE dirid=%1 AND name='%2';")
.arg(albumID)
.arg(escapeString(name)),
&values );
return !(values.isEmpty());
}
// from albuminfo.h
class TagInfo
{
public:
typedef TQValueList<TagInfo> List;
int id;
int pid;
TQString name;
TQString icon;
};
void tdeio_digikamalbums::addImage(int albumID, const TQString& filePath)
{
// Code duplication: ScanLib::storeItemInDatabase, AlbumDB::addItem,
// AlbumDB::setItemRating, AlbumDB::addItemTag, AlbumDB::addTag
// from ScanLib::storeItemInDatabase
TQString comment;
TQDateTime datetime;
int rating = 0;
Digikam::DMetadata metadata(filePath);
// Trying to get comments from image :
// In first, from standard JPEG comments, or
// In second, from EXIF comments tag, or
// In third, from IPTC comments tag.
comment = metadata.getImageComment();
// Trying to get date and time from image :
// In first, from EXIF date & time tags, or
// In second, from IPTC date & time tags.
datetime = metadata.getImageDateTime();
// Trying to get image rating from IPTC Urgency tag.
rating = metadata.getImageRating();
if (!datetime.isValid())
{
TQFileInfo info(filePath);
datetime = info.lastModified();
}
// Try to get image tags from IPTC keywords tags.
TQStringList keywordsList = metadata.getImageKeywords();
// from AlbumDB::addItem
m_sqlDB.execSql(TQString("REPLACE INTO Images "
"(dirid, name, datetime, caption) "
"VALUES(%1, '%2', '%3', '%4')")
.arg(TQString::number(albumID),
escapeString(TQFileInfo(filePath).fileName()),
datetime.toString(Qt::ISODate),
escapeString(comment)));
TQ_LLONG imageID = m_sqlDB.lastInsertedRow();
// from AlbumDB::setItemRating
if (imageID != -1 && rating != -1)
{
m_sqlDB.execSql(TQString("REPLACE INTO ImageProperties "
"(imageid, property, value) "
"VALUES(%1, '%2', '%3');")
.arg(imageID)
.arg("Rating")
.arg(rating) );
}
// Set existing tags in database or create new tags if not exist.
if ( imageID != -1 && !keywordsList.isEmpty() )
{
TQStringList keywordsList2Create;
// Create a list of the tags currently in database
TagInfo::List tagsList;
TQStringList values;
m_sqlDB.execSql( "SELECT id, pid, name FROM Tags;", &values );
for (TQStringList::iterator it = values.begin(); it != values.end();)
{
TagInfo info;
info.id = (*it).toInt();
++it;
info.pid = (*it).toInt();
++it;
info.name = *it;
++it;
tagsList.append(info);
}
// For every tag in keywordsList, scan taglist to check if tag already exists.
for (TQStringList::iterator kwd = keywordsList.begin();
kwd != keywordsList.end(); ++kwd )
{
// split full tag "url" into list of single tag names
TQStringList tagHierarchy = TQStringList::split('/', *kwd);
if (tagHierarchy.isEmpty())
continue;
// last entry in list is the actual tag name
bool foundTag = false;
TQString tagName = tagHierarchy.back();
tagHierarchy.pop_back();
for (TagInfo::List::iterator tag = tagsList.begin();
tag != tagsList.end(); ++tag )
{
// There might be multiple tags with the same name, but in different
// hierarchies. We must check them all until we find the correct hierarchy
if ((*tag).name == tagName)
{
int parentID = (*tag).pid;
// Check hierarchy, from bottom to top
bool foundParentTag = true;
TQStringList::iterator parentTagName = tagHierarchy.end();
while (foundParentTag && parentTagName != tagHierarchy.begin())
{
--parentTagName;
foundParentTag = false;
for (TagInfo::List::iterator parentTag = tagsList.begin();
parentTag != tagsList.end(); ++parentTag )
{
// check if name is the same, and if ID is identical
// to the parent ID we got from the child tag
if ( (*parentTag).id == parentID &&
(*parentTag).name == (*parentTagName) )
{
parentID = (*parentTag).pid;
foundParentTag = true;
break;
}
}
// If we traversed the list without a match,
// foundParentTag will be false, the while loop breaks.
}
// If we managed to traverse the full hierarchy,
// we have our tag.
if (foundParentTag)
{
// from AlbumDB::addItemTag
m_sqlDB.execSql( TQString("REPLACE INTO ImageTags (imageid, tagid) "
"VALUES(%1, %2);")
.arg(imageID)
.arg((*tag).id) );
foundTag = true;
break;
}
}
}
if (!foundTag)
keywordsList2Create.append(*kwd);
}
// If tags do not exist in database, create them.
if (!keywordsList2Create.isEmpty())
{
for (TQStringList::iterator kwd = keywordsList2Create.begin();
kwd != keywordsList2Create.end(); ++kwd )
{
// split full tag "url" into list of single tag names
TQStringList tagHierarchy = TQStringList::split('/', *kwd);
if (tagHierarchy.isEmpty())
continue;
int parentTagID = 0;
int tagID = 0;
bool parentTagExisted = true;
// Traverse hierarchy from top to bottom
for (TQStringList::iterator tagName = tagHierarchy.begin();
tagName != tagHierarchy.end(); ++tagName)
{
tagID = 0;
// if the parent tag did not exist, we need not check if the child exists
if (parentTagExisted)
{
for (TagInfo::List::iterator tag = tagsList.begin();
tag != tagsList.end(); ++tag )
{
// find the tag with tag name according to tagHierarchy,
// and parent ID identical to the ID of the tag we found in
// the previous run.
if ((*tag).name == (*tagName) && (*tag).pid == parentTagID)
{
tagID = (*tag).id;
break;
}
}
}
if (tagID != 0)
{
// tag already found in DB
parentTagID = tagID;
continue;
}
// Tag does not yet exist in DB, add it
// from AlbumDB::addTag
m_sqlDB.execSql( TQString("INSERT INTO Tags (pid, name, icon) "
"VALUES( %1, '%2', 0)")
.arg(parentTagID)
.arg(escapeString(*tagName)));
tagID = m_sqlDB.lastInsertedRow();
if (tagID == -1)
{
// Something is wrong in database. Abort.
break;
}
// append to our list of existing tags (for following keywords)
TagInfo info;
info.id = tagID;
info.pid = parentTagID;
info.name = (*tagName);
tagsList.append(info);
parentTagID = tagID;
parentTagExisted = false;
}
// from AlbumDB::addItemTag
m_sqlDB.execSql( TQString("REPLACE INTO ImageTags (imageid, tagid) "
"VALUES(%1, %2);")
.arg(imageID)
.arg(tagID) );
}
}
}
}
void tdeio_digikamalbums::delImage(int albumID, const TQString& name)
{
// code duplication from AlbumDB::deleteItem
m_sqlDB.execSql( TQString("DELETE FROM Images "
"WHERE dirid=%1 AND name='%2';")
.arg(albumID)
.arg(escapeString(name)) );
}
void tdeio_digikamalbums::renameImage(int oldAlbumID, const TQString& oldName,
int newAlbumID, const TQString& newName)
{
// code duplication from AlbumDB::deleteItem, AlbumDB::moveItem
// first delete any stale entries for the destination file
m_sqlDB.execSql( TQString("DELETE FROM Images "
"WHERE dirid=%1 AND name='%2';")
.arg(newAlbumID)
.arg(escapeString(newName)) );
// now update the dirid and/or name of the file
m_sqlDB.execSql( TQString("UPDATE Images SET dirid=%1, name='%2' "
"WHERE dirid=%3 AND name='%4';")
.arg(TQString::number(newAlbumID),
escapeString(newName),
TQString::number(oldAlbumID),
escapeString(oldName)) );
}
void tdeio_digikamalbums::copyImage(int srcAlbumID, const TQString& srcName,
int dstAlbumID, const TQString& dstName)
{
// code duplication from AlbumDB::copyItem
// check for src == dest
if (srcAlbumID == dstAlbumID && srcName == dstName)
{
error( TDEIO::ERR_FILE_ALREADY_EXIST, dstName );
return;
}
// find id of src image
TQStringList values;
m_sqlDB.execSql( TQString("SELECT id FROM Images "
"WHERE dirid=%1 AND name='%2';")
.arg(TQString::number(srcAlbumID), escapeString(srcName)),
&values);
if (values.isEmpty())
{
error(TDEIO::ERR_UNKNOWN, i18n("Source image %1 not found in database")
.arg(srcName));
return;
}
int srcId = values[0].toInt();
// first delete any stale entries for the destination file
m_sqlDB.execSql( TQString("DELETE FROM Images "
"WHERE dirid=%1 AND name='%2';")
.arg(TQString::number(dstAlbumID), escapeString(dstName)) );
// copy entry in Images table
m_sqlDB.execSql( TQString("INSERT INTO Images (dirid, name, caption, datetime) "
"SELECT %1, '%2', caption, datetime FROM Images "
"WHERE id=%3;")
.arg(TQString::number(dstAlbumID), escapeString(dstName),
TQString::number(srcId)) );
int dstId = m_sqlDB.lastInsertedRow();
// copy tags
m_sqlDB.execSql( TQString("INSERT INTO ImageTags (imageid, tagid) "
"SELECT %1, tagid FROM ImageTags "
"WHERE imageid=%2;")
.arg(TQString::number(dstId), TQString::number(srcId)) );
// copy properties (rating)
m_sqlDB.execSql( TQString("INSERT INTO ImageProperties (imageid, property, value) "
"SELECT %1, property, value FROM ImageProperties "
"WHERE imageid=%2;")
.arg(TQString::number(dstId), TQString::number(srcId)) );
}
void tdeio_digikamalbums::scanAlbum(const TQString& url)
{
scanOneAlbum(url);
removeInvalidAlbums();
}
void tdeio_digikamalbums::scanOneAlbum(const TQString& url)
{
TQDir dir(m_libraryPath + url);
if (!dir.exists() || !dir.isReadable())
{
return;
}
TQString subURL = url;
if (!url.endsWith("/"))
subURL += '/';
subURL = escapeString( subURL);
{
// scan albums
TQStringList currAlbumList;
m_sqlDB.execSql( TQString("SELECT url FROM Albums WHERE ") +
TQString("url LIKE '") + subURL + TQString("%' ") +
TQString("AND url NOT LIKE '") + subURL + TQString("%/%' "),
&currAlbumList );
const TQFileInfoList* infoList = dir.entryInfoList(TQDir::Dirs);
if (!infoList)
return;
TQFileInfoListIterator it(*infoList);
TQFileInfo* fi;
TQStringList newAlbumList;
while ((fi = it.current()) != 0)
{
++it;
if (fi->fileName() == "." || fi->fileName() == "..")
{
continue;
}
TQString u = TQDir::cleanDirPath(url + '/' + fi->fileName());
if (currAlbumList.contains(u))
{
continue;
}
newAlbumList.append(u);
}
for (TQStringList::iterator it = newAlbumList.begin();
it != newAlbumList.end(); ++it)
{
kdDebug() << "New Album: " << *it << endl;
TQFileInfo fi(m_libraryPath + *it);
m_sqlDB.execSql(TQString("INSERT INTO Albums (url, date) "
"VALUES('%1', '%2')")
.arg(escapeString(*it),
fi.lastModified().date().toString(Qt::ISODate)));
scanAlbum(*it);
}
}
if (url != "/")
{
// scan files
TQStringList values;
m_sqlDB.execSql( TQString("SELECT id FROM Albums WHERE url='%1'")
.arg(escapeString(url)), &values );
if (values.isEmpty())
return;
int albumID = values.first().toInt();
TQStringList currItemList;
m_sqlDB.execSql( TQString("SELECT name FROM Images WHERE dirid=%1")
.arg(albumID), &currItemList );
const TQFileInfoList* infoList = dir.entryInfoList(TQDir::Files);
if (!infoList)
return;
TQFileInfoListIterator it(*infoList);
TQFileInfo* fi;
// add any new files we find to the db
while ((fi = it.current()) != 0)
{
++it;
// ignore temp files we created ourselves
if (fi->extension(true) == "digikamtempfile.tmp")
{
continue;
}
if (currItemList.contains(fi->fileName()))
{
currItemList.remove(fi->fileName());
continue;
}
addImage(albumID, m_libraryPath + url + '/' + fi->fileName());
}
// currItemList now contains deleted file list. remove them from db
for (TQStringList::iterator it = currItemList.begin();
it != currItemList.end(); ++it)
{
delImage(albumID, *it);
}
}
}
void tdeio_digikamalbums::removeInvalidAlbums()
{
TQStringList urlList;
m_sqlDB.execSql(TQString("SELECT url FROM Albums;"),
&urlList);
m_sqlDB.execSql("BEGIN TRANSACTION");
struct stat stbuf;
for (TQStringList::iterator it = urlList.begin();
it != urlList.end(); ++it)
{
if (::stat(TQFile::encodeName(m_libraryPath + *it), &stbuf) == 0)
continue;
kdDebug() << "Deleted Album: " << *it << endl;
m_sqlDB.execSql(TQString("DELETE FROM Albums WHERE url='%1'")
.arg(escapeString(*it)));
}
m_sqlDB.execSql("COMMIT TRANSACTION");
}
/* KIO slave registration */
extern "C"
{
DIGIKAM_EXPORT int kdemain(int argc, char **argv)
{
TDELocale::setMainCatalogue("digikam");
TDEInstance instance( "tdeio_digikamalbums" );
TDEGlobal::locale();
if (argc != 4) {
kdDebug() << "Usage: tdeio_digikamalbums protocol domain-socket1 domain-socket2"
<< endl;
exit(-1);
}
tdeio_digikamalbums slave(argv[2], argv[3]);
slave.dispatchLoop();
return 0;
}
}
|