/
Album.java
806 lines (661 loc) · 17.5 KB
/
Album.java
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
/*
* Gallery Remote - a File Upload Utility for Gallery
*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2001 Bharat Mediratta
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package com.gallery.GalleryRemote.model;
import com.gallery.GalleryRemote.*;
import com.gallery.GalleryRemote.util.GRI18n;
import com.gallery.GalleryRemote.util.ImageUtils;
import com.gallery.GalleryRemote.util.NaturalOrderComparator;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.Serializable;
import java.util.*;
import java.util.List;
/**
* Album model
*
* @author paour
* @created 11 août 2002
*/
public class Album extends Picture implements ListModel, Serializable {
/* -------------------------------------------------------------------------
* CONSTANTS
*/
public static final String MODULE = "Album";
/* -------------------------------------------------------------------------
* LOCAL STORAGE
*/
ArrayList pictures = new ArrayList();
/* -------------------------------------------------------------------------
* SERVER INFO
*/
Gallery gallery = null;
ArrayList subAlbums = new ArrayList();
Album parent; // parent Album
String title = GRI18n.getString(MODULE, "title");
String name;
ArrayList extraFields;
String summary;
Boolean overrideResize = null;
Boolean overrideResizeDefault = null;
Dimension overrideResizeDimension = null;
Boolean overrideAddToBeginning = null;
int autoResize = 0;
// permissions -- default to true for the sake of old protocols ...
boolean canRead = true;
boolean canAdd = true;
boolean canWrite = true;
boolean canDeleteFrom = true;
boolean canDeleteThisAlbum = true;
boolean canCreateSubAlbum = true;
boolean hasFetchedInfo = false;
boolean hasFetchedImages = false;
transient private Long pictureFileSize;
transient private Integer albumDepth;
public static List extraFieldsNoShow = Arrays.asList(new String[]{GRI18n.getString(MODULE, "upDate"), GRI18n.getString(MODULE, "captDate")});
public Album(Gallery gallery) {
this.gallery = gallery;
}
/**
* Retrieves the album properties from the server.
*
* @param gallery The new gallery
*/
public void fetchAlbumProperties(StatusUpdate su) {
if (!hasFetchedInfo && getGallery().getComm(su).hasCapability(GalleryCommCapabilities.CAPA_ALBUM_INFO)) {
if (su == null) {
su = new StatusUpdateAdapter() {
};
}
try {
gallery.getComm(su).albumInfo(su, this, false);
} catch (RuntimeException e) {
Log.log(Log.LEVEL_INFO, MODULE, "Server probably doesn't support album-info");
Log.logException(Log.LEVEL_INFO, MODULE, e);
}
}
}
public void fetchAlbumImages(StatusUpdate su) {
if (getGallery().getComm(su).hasCapability(GalleryCommCapabilities.CAPA_FETCH_ALBUM_IMAGES)) {
if (su == null) {
su = new StatusUpdateAdapter() {
};
}
try {
gallery.getComm(su).fetchAlbumImages(su, this, false);
} catch (RuntimeException e) {
Log.log(Log.LEVEL_INFO, MODULE, "Server probably doesn't support album-fetch-images");
Log.logException(Log.LEVEL_INFO, MODULE, e);
}
}
}
public void moveAlbumTo(StatusUpdate su, Album newParent) {
if (getGallery().getComm(su).hasCapability(GalleryCommCapabilities.CAPA_MOVE_ALBUM)) {
if (su == null) {
su = new StatusUpdateAdapter() {
};
}
try {
gallery.getComm(su).moveAlbum(su, this, newParent, false);
gallery.fetchAlbums(su);
} catch (RuntimeException e) {
Log.log(Log.LEVEL_INFO, MODULE, "Server probably doesn't support move-album");
Log.logException(Log.LEVEL_INFO, MODULE, e);
}
}
}
/**
* Sets the server auto resize dimension.
*
* @param autoResize the server's resize dimension
*/
public void setServerAutoResize(int autoResize) {
this.autoResize = autoResize;
hasFetchedInfo = true;
}
/**
* Gets the server auto resize dimension.
*
* @return the server's resize dimension for this album
*/
public int getServerAutoResize() {
fetchAlbumProperties(null);
return autoResize;
}
/**
* Sets the gallery attribute of the Album object
*
* @param gallery The new gallery
*/
public void setGallery(Gallery gallery) {
this.gallery = gallery;
}
/**
* Gets the gallery attribute of the Album object
*
* @return The gallery
*/
public Gallery getGallery() {
return gallery;
}
/**
* Gets the pictures inside the album
*
* @return The pictures value
*/
public Iterator getPictures() {
return pictures.iterator();
}
/**
* Adds a picture to the album
*
* @param p the picture to add. This will change its parent album
*/
public void addPicture(Picture p) {
p.setAlbum(this);
addPictureInternal(p);
notifyListeners();
}
/**
* Adds a picture to the album
*
* @param file the file to create the picture from
*/
public Picture addPicture(File file) {
Picture p = new Picture(file);
p.setAlbum(this);
addPictureInternal(p);
notifyListeners();
return p;
}
/**
* Adds pictures to the album
*
* @param files the files to create the pictures from
*/
public ArrayList addPictures(File[] files) {
return addPictures(files, -1);
}
/**
* Adds pictures to the album at a specified index
*
* @param files the files to create the pictures from
* @param index the index in the list at which to begin adding
*/
public ArrayList addPictures(File[] files, int index) {
ArrayList pictures = new ArrayList(files.length);
for (int i = 0; i < files.length; i++) {
Picture p = new Picture(files[i]);
p.setAlbum(this);
if (index == -1) {
addPictureInternal(p);
} else {
addPictureInternal(index++, p);
}
pictures.add(p);
}
notifyListeners();
return pictures;
}
/**
* Adds picturesA to the album
*
* @param picturesA the picturesA
*/
public void addPictures(List picturesL) {
addPictures(picturesL, -1);
}
public void addPictures(List picturesL, int index) {
for (Iterator it = picturesL.iterator(); it.hasNext();) {
Picture p = (Picture) it.next();
p.setAlbum(this);
if (index == -1) {
pictures.add(p);
} else {
pictures.add(index++, p);
}
}
notifyListeners();
}
private void addPictureInternal(Picture p) {
addPictureInternal(-1, p);
}
private void addPictureInternal(int index, Picture p) {
// handle EXIF
if (GalleryRemote.getInstance().properties.getBooleanProperty(EXIF_AUTOROTATE)) {
ImageUtils.AngleFlip af = ImageUtils.getExifTargetOrientation(p.getSource().getPath());
if (af != null) {
p.setFlipped(af.flip);
p.setAngle(af.angle);
p.setSuppressServerAutoRotate(true);
}
}
if (index == -1) {
pictures.add(p);
} else {
pictures.add(index, p);
}
}
public void sortPicturesAlphabetically() {
Collections.sort(pictures, new NaturalOrderComparator());
notifyListeners();
}
/**
* Number of pictures in the album
*
* @return Number of pictures in the album
*/
public int sizePictures() {
return pictures.size();
}
/**
* Remove all the pictures
*/
public void clearPictures() {
pictures.clear();
notifyListeners();
}
/**
* Remove a picture
*
* @param n item number of the picture to remove
*/
public void removePicture(int n) {
pictures.remove(n);
fireIntervalRemoved(this, n, n);
}
public void removePicture(Picture p) {
removePicture(pictures.indexOf(p));
}
/**
* Remove pictures
*
* @param indices list of indices of pictures to remove
*/
public void removePictures(int[] indices) {
int min, max;
min = max = indices[0];
for (int i = indices.length - 1; i >= 0; i--) {
pictures.remove(indices[i]);
if (indices[i] > max) max = indices[i];
if (indices[i] < min) min = indices[i];
}
fireIntervalRemoved(this, min, max);
}
/**
* Get a picture from the album
*
* @param n index of the picture to retrieve
* @return The Picture
*/
public Picture getPicture(int n) {
return (Picture) pictures.get(n);
}
/**
* Set a picture in the album
*
* @param n index of the picture
* @param p The new picture
*/
public void setPicture(int n, Picture p) {
pictures.set(n, p);
notifyListeners();
}
/**
* Get the list of files that contain the pictures
*
*@return The fileList value
*/
/*public ArrayList getFileList() {
ArrayList l = new ArrayList( pictures.size() );
Enumeration e = pictures.elements();
while ( e.hasMoreElements() ) {
l.add( ( (Picture) e.nextElement() ).getSource() );
}
return l;
}*/
/**
* Sets the name attribute of the Album object
*
* @param name The new name value
*/
public void setName(String name) {
this.name = removeOffendingChars(name);
}
static final String offendingChars = "\\/*?\"\'&<>|.+# ";
static String removeOffendingChars(String in) {
StringBuffer out = new StringBuffer();
int l = in.length();
for (int i = 0; i < l; i++) {
char c = in.charAt(i);
if (offendingChars.indexOf(c) == -1) {
out.append(c);
}
}
return out.toString();
}
/**
* Gets the name attribute of the Album object
*
* @return The name value
*/
public String getName() {
return name;
}
/**
* Sets the title attribute of the Album object
*
* @param title The new title
*/
public void setTitle(String title) {
this.title = title;
gallery.albumChanged(this);
}
/**
* Gets the title attribute of the Album object
*
* @return The title
*/
public String getTitle() {
return title;
}
/**
* Gets the aggregated file size of all the pictures in the album
*
* @return The file size (bytes)
*/
public long getPictureFileSize() {
if (pictureFileSize == null) {
pictureFileSize = new Long(getPictureFileSize((Picture[]) pictures.toArray(new Picture[0])));
}
return pictureFileSize.longValue();
}
/**
* Gets the aggregated file size of a list of pictures
*
* @param pictures the list of Pictures
* @return The file size (bytes)
*/
public static long getPictureFileSize(Picture[] pictures) {
return getObjectFileSize(pictures);
}
/**
* Gets the aggregated file size of a list of pictures Unsafe, the Objects
* will be cast to Pictures.
*
* @param pictures the list of Pictures
* @return The file size (bytes)
*/
public static long getObjectFileSize(Object[] pictures) {
long total = 0;
for (int i = 0; i < pictures.length; i++) {
total += ((Picture) pictures[i]).getFileSize();
}
return total;
}
public String toString() {
StringBuffer ret = new StringBuffer();
ret.append(indentHelper(""));
ret.append(title);
if (pictures.size() != 0) {
ret.append(" (" + pictures.size() + ")");
}
// using canAdd here, since that's the only operation we perform
// currently. eventually, when we start changing things
// on the server, permission support will get more ... interesting.
if (!canAdd) {
ret.append(" ").append(GRI18n.getString(MODULE, "ro"));
}
return ret.toString();
}
public boolean equals(Object o) {
return (o != null
&& o instanceof Album
&& ((Album) o).getGallery() == getGallery()
&& getName() != null
&& ((Album) o).getName() != null
&& ((Album) o).getName().equals(getName()));
}
/* -------------------------------------------------------------------------
*ListModel Implementation
*/
/**
* Gets the size attribute of the Album object
*
* @return The size value
*/
public int getSize() {
return pictures.size();
}
/**
* Gets the elementAt attribute of the Album object
*
* @param index Description of Parameter
* @return The elementAt value
*/
public Object getElementAt(int index) {
return pictures.get(index);
}
/**
* Description of the Method
*
* @param ldl Description of Parameter
*/
public Album getParentAlbum() {
return parent;
}
/**
* Description of the Method
*
* @param ldl Description of Parameter
*/
public void setParentAlbum(Album a) {
// take care of a Gallery bug...
if (a == this) {
Log.log(Log.LEVEL_ERROR, MODULE, "Gallery error: the album " + name +
" is its own parent. You should delete it, the album database " +
"is corrupted because of it.");
a = null;
}
parent = a;
if (a != null) {
if (!a.subAlbums.contains(this)) {
a.subAlbums.add(this);
}
} else {
if (!gallery.rootAlbums.contains(this)) {
gallery.rootAlbums.add(this);
}
}
}
public ArrayList getExtraFields() {
return extraFields;
}
public void setExtraFieldsString(String extraFieldsString) {
if (extraFieldsString != null && extraFieldsString.length() > 0) {
extraFields = new ArrayList();
StringTokenizer st = new StringTokenizer(extraFieldsString, ",");
while (st.hasMoreTokens()) {
String name = st.nextToken();
if (!extraFieldsNoShow.contains(name)) {
extraFields.add(name);
}
}
} else {
extraFields = null;
}
}
public void setCanRead(boolean b) {
canRead = b;
}
public boolean getCanRead() {
return canRead;
}
public void setCanAdd(boolean b) {
canAdd = b;
}
public boolean getCanAdd() {
return canAdd;
}
public void setCanWrite(boolean b) {
canWrite = b;
}
public boolean getCanWrite() {
return canWrite;
}
public void setCanDeleteFrom(boolean b) {
canDeleteFrom = b;
}
public boolean getCanDeleteFrom() {
return canDeleteFrom;
}
public void setCanDeleteThisAlbum(boolean b) {
canDeleteThisAlbum = b;
}
public boolean getCanDeleteThisAlbum() {
return canDeleteThisAlbum;
}
public void setCanCreateSubAlbum(boolean b) {
canCreateSubAlbum = b;
}
public boolean getCanCreateSubAlbum() {
return canCreateSubAlbum;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public ArrayList getPicturesList() {
return pictures;
}
ArrayList getUploadablePicturesList() {
ArrayList uploadable = new ArrayList();
for (Iterator it = pictures.iterator(); it.hasNext();) {
Picture picture = (Picture) it.next();
if (!picture.isOnline()) {
uploadable.add(picture);
}
}
return uploadable;
}
void setPicturesList(ArrayList pictures) {
this.pictures = pictures;
for (Iterator e = pictures.iterator(); e.hasNext();) {
((Picture) e.next()).setAlbum(this);
}
notifyListeners();
}
public int getAlbumDepth() throws IllegalArgumentException {
if (albumDepth == null) {
albumDepth = new Integer(depthHelper(0));
}
return albumDepth.intValue();
}
int depthHelper(int depth) throws IllegalArgumentException {
if (getParentAlbum() == this || depth > 20) {
throw new IllegalArgumentException("Circular containment hierarchy. Gallery corrupted!");
}
if (getParentAlbum() != null) {
return getParentAlbum().depthHelper(depth + 1);
} else {
return depth;
}
}
public static final String INDENT_QUANTUM = " ";
String indentHelper(String indent) {
if (getParentAlbum() != null) {
return getParentAlbum().indentHelper(indent + INDENT_QUANTUM);
} else {
return indent;
}
}
void notifyListeners() {
fireContentsChanged(this, 0, pictures.size());
gallery.albumChanged(this);
}
public ArrayList getSubAlbums() {
return subAlbums;
}
public Boolean getOverrideResize() {
return overrideResize;
}
public void setOverrideResize(Boolean overrideResize) {
this.overrideResize = overrideResize;
}
public Boolean getOverrideResizeDefault() {
return overrideResizeDefault;
}
public void setOverrideResizeDefault(Boolean overrideResizeDefault) {
this.overrideResizeDefault = overrideResizeDefault;
}
public Dimension getOverrideResizeDimension() {
return overrideResizeDimension;
}
public void setOverrideResizeDimension(Dimension overrideResizeDimension) {
this.overrideResizeDimension = overrideResizeDimension;
}
public Boolean getOverrideAddToBeginning() {
return overrideAddToBeginning;
}
public void setOverrideAddToBeginning(Boolean overrideAddToBeginning) {
this.overrideAddToBeginning = overrideAddToBeginning;
}
public boolean getResize() {
if (overrideResize != null) {
return overrideResize.booleanValue();
} else {
return GalleryRemote.getInstance().properties.getBooleanProperty(RESIZE_BEFORE_UPLOAD);
}
}
public boolean getResizeDefault() {
if (overrideResizeDefault != null) {
return overrideResizeDefault.booleanValue();
} else {
return new Dimension(0, 0).equals(GalleryRemote.getInstance().properties.getDimensionProperty(RESIZE_TO));
}
}
public Dimension getResizeDimension() {
if (overrideResizeDimension != null) {
return overrideResizeDimension;
} else {
return GalleryRemote.getInstance().properties.getDimensionProperty(RESIZE_TO);
}
}
public boolean getAddToBeginning() {
if (overrideAddToBeginning != null) {
return overrideAddToBeginning.booleanValue();
} else {
// todo
return false;
}
}
public boolean isHasFetchedImages() {
return hasFetchedImages;
}
public void setHasFetchedImages(boolean hasFetchedImages) {
this.hasFetchedImages = hasFetchedImages;
}
/*public void checkTransients() {
subAlbums = new ArrayList();
}*/
}