aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/cgCache.java
blob: 220f2fa9cfa95d7aef466a7f0aed609f84fe0c8e (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
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
package cgeo.geocaching;

import cgeo.geocaching.cgData.StorageLocation;
import cgeo.geocaching.activity.IAbstractActivity;
import cgeo.geocaching.connector.ConnectorFactory;
import cgeo.geocaching.connector.GCConnector;
import cgeo.geocaching.connector.IConnector;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.geopoint.GeopointFormatter;
import cgeo.geocaching.geopoint.GeopointParser;
import cgeo.geocaching.utils.CryptUtils;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.text.Spannable;
import android.util.Log;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Internal c:geo representation of a "cache"
 */
public class cgCache implements ICache {

    private long updated = 0;
    private long detailedUpdate = 0;
    private long visitedDate = 0;
    private int listId = 0;
    private boolean detailed = false;
    private String geocode = "";
    private String cacheId = "";
    private String guid = "";
    private CacheType cacheType = CacheType.UNKNOWN;
    private String name = "";
    private Spannable nameSp = null;
    private String owner = "";
    private String ownerReal = "";
    private Date hidden = null;
    private String hint = "";
    private CacheSize size = null;
    private float difficulty = 0;
    private float terrain = 0;
    private Float direction = null;
    private Float distance = null;
    private String latlon = "";
    private String location = "";
    private Geopoint coords = null;
    private boolean reliableLatLon = false;
    private Double elevation = null;
    private String personalNote = null;
    private String shortdesc = "";
    private String description = null;
    private boolean disabled = false;
    private boolean archived = false;
    private boolean premiumMembersOnly = false;
    private boolean found = false;
    private boolean favorite = false;
    private boolean own = false;
    private int favoritePoints = 0;
    private float rating = 0; // valid ratings are larger than zero
    private int votes = 0;
    private float myVote = 0; // valid ratings are larger than zero
    private int inventoryItems = 0;
    private boolean onWatchlist = false;
    private List<String> attributes = null;
    private List<cgWaypoint> waypoints = null;
    private ArrayList<cgImage> spoilers = null;
    private List<cgLog> logs = null;
    private List<cgTrackable> inventory = null;
    private Map<LogType, Integer> logCounts = new HashMap<LogType, Integer>();
    private boolean logOffline = false;
    // temporary values
    private boolean statusChecked = false;
    private boolean statusCheckedView = false;
    private String directionImg = "";
    private String nameForSorting;
    private final EnumSet<StorageLocation> storageLocation = EnumSet.of(StorageLocation.HEAP);

    private static final Pattern NUMBER_PATTERN = Pattern.compile("\\d+");

    /**
     * Gather missing information from another cache object.
     *
     * @param other
     *            the other version, or null if non-existent
     * @return true if this cache is "equal" to the other version
     */
    public boolean gatherMissingFrom(final cgCache other) {
        if (other == null) {
            return false;
        }

        updated = System.currentTimeMillis();
        if (!detailed && other.detailed) {
            detailed = true;
            detailedUpdate = other.detailedUpdate;
            coords = other.coords;
            premiumMembersOnly = other.premiumMembersOnly;
            reliableLatLon = other.reliableLatLon;
            archived = other.archived;
            favorite = other.favorite;
            onWatchlist = other.onWatchlist;
            logOffline = other.logOffline;
        }

        /*
         * No gathering for boolean members
         * - found
         * - own
         * - disabled
         * - favorite
         * - onWatchlist
         * - logOffline
         */
        if (visitedDate == 0) {
            visitedDate = other.getVisitedDate();
        }
        if (listId == 0) {
            listId = other.listId;
        }
        if (StringUtils.isBlank(geocode)) {
            geocode = other.getGeocode();
        }
        if (StringUtils.isBlank(cacheId)) {
            cacheId = other.cacheId;
        }
        if (StringUtils.isBlank(guid)) {
            guid = other.getGuid();
        }
        if (null == cacheType || CacheType.UNKNOWN == cacheType) {
            cacheType = other.getType();
        }
        if (StringUtils.isBlank(name)) {
            name = other.getName();
        }
        if (StringUtils.isBlank(nameSp)) {
            nameSp = other.nameSp;
        }
        if (StringUtils.isBlank(owner)) {
            owner = other.getOwner();
        }
        if (StringUtils.isBlank(ownerReal)) {
            ownerReal = other.getOwnerReal();
        }
        if (hidden == null) {
            hidden = other.hidden;
        }
        if (StringUtils.isBlank(hint)) {
            hint = other.hint;
        }
        if (size == null) {
            size = other.size;
        }
        if (difficulty == 0) {
            difficulty = other.getDifficulty();
        }
        if (terrain == 0) {
            terrain = other.getTerrain();
        }
        if (direction == null) {
            direction = other.direction;
        }
        if (distance == null) {
            distance = other.getDistance();
        }
        if (StringUtils.isBlank(latlon)) {
            latlon = other.latlon;
        }
        if (StringUtils.isBlank(location)) {
            location = other.location;
        }
        if (coords == null) {
            coords = other.getCoords();
        }
        if (elevation == null) {
            elevation = other.elevation;
        }
        if (personalNote == null) { // don't use StringUtils.isBlank here. Otherwise we cannot recognize a note which was deleted on GC
            personalNote = other.personalNote;
        }
        if (StringUtils.isBlank(shortdesc)) {
            shortdesc = other.getShortdesc();
        }
        if (StringUtils.isBlank(description)) {
            description = other.description;
        }
        if (favoritePoints == 0) {
            favoritePoints = other.getFavoritePoints();
        }
        if (rating == 0) {
            rating = other.getRating();
        }
        if (votes == 0) {
            votes = other.votes;
        }
        if (myVote == 0) {
            myVote = other.getMyVote();
        }
        if (attributes == null) {
            attributes = other.attributes;
        }
        if (waypoints == null) {
            waypoints = other.waypoints;
        }
        else {
            cgWaypoint.mergeWayPoints(waypoints, other.getWaypoints(), waypoints == null || waypoints.isEmpty());
        }
        if (spoilers == null) {
            spoilers = other.spoilers;
        }
        if (inventory == null) {
            // If inventoryItems is 0, it can mean both
            // "don't know" or "0 items". Since we cannot distinguish
            // them here, only populate inventoryItems from
            // old data when we have to do it for inventory.
            inventory = other.inventory;
            inventoryItems = other.inventoryItems;
        }
        if (CollectionUtils.isEmpty(logs)) { // keep last known logs if none
            logs = other.logs;
        }
        if (logCounts.size() == 0) {
            logCounts = other.logCounts;
        }

        return isEqualTo(other);
    }

    /**
     * Compare two caches quickly. For map and list fields only the references are compared !
     *
     * @param other
     * @return true if both caches have the same content
     */
    public boolean isEqualTo(cgCache other) {
        if (other == null) {
            return false;
        }

        if (
        // updated
        // detailedUpdate
        // visitedDate
        detailed == other.detailed &&
                geocode.equalsIgnoreCase(other.geocode) &&
                name.equalsIgnoreCase(other.name) &&
                cacheType == other.cacheType &&
                size == other.size &&
                found == other.found &&
                own == other.own &&
                premiumMembersOnly == other.premiumMembersOnly &&
                difficulty == other.difficulty &&
                terrain == other.terrain &&
                (coords != null ? coords.isEqualTo(other.coords) : coords == other.coords) &&
                reliableLatLon == other.reliableLatLon &&
                disabled == other.disabled &&
                archived == other.archived &&
                listId == other.listId &&
                owner.equalsIgnoreCase(other.owner) &&
                ownerReal.equalsIgnoreCase(other.ownerReal) &&
                (description != null ? description.equalsIgnoreCase(other.description) : description == other.description) &&
                (personalNote != null ? personalNote.equalsIgnoreCase(other.personalNote) : personalNote == other.personalNote) &&
                shortdesc.equalsIgnoreCase(other.shortdesc) &&
                latlon.equalsIgnoreCase(other.latlon) &&
                location.equalsIgnoreCase(other.location) &&
                favorite == other.favorite &&
                favoritePoints == other.favoritePoints &&
                onWatchlist == other.onWatchlist &&
                (hidden != null ? hidden.compareTo(other.hidden) == 0 : hidden == other.hidden) &&
                guid.equalsIgnoreCase(other.guid) &&
                hint.equalsIgnoreCase(other.hint) &&
                cacheId.equalsIgnoreCase(other.cacheId) &&
                direction == other.direction &&
                distance == other.distance &&
                elevation == other.elevation &&
                nameSp == other.nameSp &&
                rating == other.rating &&
                votes == other.votes &&
                myVote == other.myVote &&
                inventoryItems == other.inventoryItems &&
                attributes == other.attributes &&
                waypoints == other.waypoints &&
                spoilers == other.spoilers &&
                logs == other.logs &&
                inventory == other.inventory &&
                logCounts == other.logCounts &&
                logOffline == other.logOffline) {
            return true;
        }
        return false;
    }

    public boolean hasTrackables() {
        return inventoryItems > 0;
    }

    public boolean canBeAddedToCalendar() {
        // is event type?
        if (!isEventCache()) {
            return false;
        }
        // has event date set?
        if (hidden == null) {
            return false;
        }
        // is not in the past?
        final Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        if (hidden.compareTo(cal.getTime()) < 0) {
            return false;
        }
        return true;
    }

    /**
     * checks if a page contains the guid of a cache
     *
     * @param cache
     *            the cache to look for
     * @param page
     *            the page to search in
     *
     * @return true: page contains guid of cache, false: otherwise
     */
    boolean isGuidContainedInPage(final String page) {
        if (StringUtils.isBlank(page)) {
            return false;
        }
        // check if the guid of the cache is anywhere in the page
        if (StringUtils.isBlank(guid)) {
            return false;
        }
        Pattern patternOk = Pattern.compile(guid, Pattern.CASE_INSENSITIVE);
        Matcher matcherOk = patternOk.matcher(page);
        if (matcherOk.find()) {
            Log.i(Settings.tag, "cgCache.isGuidContainedInPage: guid '" + guid + "' found");
            return true;
        } else {
            Log.i(Settings.tag, "cgCache.isGuidContainedInPage: guid '" + guid + "' not found");
            return false;
        }
    }

    public boolean isEventCache() {
        return CacheType.EVENT == cacheType || CacheType.MEGA_EVENT == cacheType
                || CacheType.CITO == cacheType || CacheType.LOSTANDFOUND == cacheType;
    }

    public boolean logVisit(IAbstractActivity fromActivity) {
        if (StringUtils.isBlank(cacheId)) {
            fromActivity.showToast(((Activity) fromActivity).getResources().getString(R.string.err_cannot_log_visit));
            return true;
        }
        Intent logVisitIntent = new Intent((Activity) fromActivity, VisitCacheActivity.class);
        logVisitIntent.putExtra(VisitCacheActivity.EXTRAS_ID, cacheId);
        logVisitIntent.putExtra(VisitCacheActivity.EXTRAS_GEOCODE, geocode.toUpperCase());
        logVisitIntent.putExtra(VisitCacheActivity.EXTRAS_FOUND, found);

        ((Activity) fromActivity).startActivity(logVisitIntent);

        return true;
    }

    public boolean logOffline(final IAbstractActivity fromActivity, final LogType logType) {
        String log = "";
        if (StringUtils.isNotBlank(Settings.getSignature())
                && Settings.isAutoInsertSignature()) {
            log = LogTemplateProvider.applyTemplates(Settings.getSignature(), true);
        }
        logOffline(fromActivity, log, Calendar.getInstance(), logType);
        return true;
    }

    void logOffline(final IAbstractActivity fromActivity, final String log, Calendar date, final LogType logType) {
        if (logType == LogType.LOG_UNKNOWN) {
            return;
        }
        cgeoapplication app = (cgeoapplication) ((Activity) fromActivity).getApplication();
        final boolean status = app.saveLogOffline(geocode, date.getTime(), logType, log);

        Resources res = ((Activity) fromActivity).getResources();
        if (status) {
            fromActivity.showToast(res.getString(R.string.info_log_saved));
            app.saveVisitDate(geocode);
        } else {
            fromActivity.showToast(res.getString(R.string.err_log_post_failed));
        }
    }

    public List<LogType> getPossibleLogTypes() {
        boolean isOwner = owner != null && owner.equalsIgnoreCase(Settings.getUsername());
        List<LogType> logTypes = new ArrayList<LogType>();
        if (isEventCache()) {
            logTypes.add(LogType.LOG_WILL_ATTEND);
            logTypes.add(LogType.LOG_NOTE);
            logTypes.add(LogType.LOG_ATTENDED);
            logTypes.add(LogType.LOG_NEEDS_ARCHIVE);
            if (isOwner) {
                logTypes.add(LogType.LOG_ANNOUNCEMENT);
            }
        } else if (CacheType.WEBCAM == cacheType) {
            logTypes.add(LogType.LOG_WEBCAM_PHOTO_TAKEN);
            logTypes.add(LogType.LOG_DIDNT_FIND_IT);
            logTypes.add(LogType.LOG_NOTE);
            logTypes.add(LogType.LOG_NEEDS_ARCHIVE);
            logTypes.add(LogType.LOG_NEEDS_MAINTENANCE);
        } else {
            logTypes.add(LogType.LOG_FOUND_IT);
            logTypes.add(LogType.LOG_DIDNT_FIND_IT);
            logTypes.add(LogType.LOG_NOTE);
            logTypes.add(LogType.LOG_NEEDS_ARCHIVE);
            logTypes.add(LogType.LOG_NEEDS_MAINTENANCE);
        }
        if (isOwner) {
            logTypes.add(LogType.LOG_OWNER_MAINTENANCE);
            logTypes.add(LogType.LOG_TEMP_DISABLE_LISTING);
            logTypes.add(LogType.LOG_ENABLE_LISTING);
            logTypes.add(LogType.LOG_ARCHIVE);
            logTypes.remove(LogType.LOG_UPDATE_COORDINATES);
        }
        return logTypes;
    }

    public void openInBrowser(Activity fromActivity) {
        fromActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getCacheUrl())));
    }

    private String getCacheUrl() {
        return getConnector().getCacheUrl(this);
    }

    private IConnector getConnector() {
        return ConnectorFactory.getConnector(this);
    }

    public boolean canOpenInBrowser() {
        return getCacheUrl() != null;
    }

    public boolean supportsRefresh() {
        return getConnector().supportsRefreshCache(this);
    }

    public boolean supportsWatchList() {
        return getConnector().supportsWatchList();
    }

    public boolean supportsLogging() {
        return getConnector().supportsLogging();
    }

    @Override
    public float getDifficulty() {
        return difficulty;
    }

    @Override
    public String getGeocode() {
        return geocode;
    }

    @Override
    public String getLatitude() {
        return coords != null ? coords.format(GeopointFormatter.Format.LAT_DECMINUTE) : null;
    }

    @Override
    public String getLongitude() {
        return coords != null ? coords.format(GeopointFormatter.Format.LON_DECMINUTE) : null;
    }

    @Override
    public String getOwner() {
        return owner;
    }

    @Override
    public CacheSize getSize() {
        return size;
    }

    @Override
    public float getTerrain() {
        return terrain;
    }

    @Override
    public boolean isArchived() {
        return archived;
    }

    @Override
    public boolean isDisabled() {
        return disabled;
    }

    @Override
    public boolean isPremiumMembersOnly() {
        return premiumMembersOnly;
    }

    public void setPremiumMembersOnly(boolean members) {
        this.premiumMembersOnly = members;
    }

    @Override
    public boolean isOwn() {
        return own;
    }

    @Override
    public String getOwnerReal() {
        return ownerReal;
    }

    @Override
    public String getHint() {
        return hint;
    }

    @Override
    public String getDescription() {
        if (description == null) {
            description = StringUtils.defaultString(cgeoapplication.getInstance().getCacheDescription(geocode));
        }
        return description;
    }

    @Override
    public String getShortDescription() {
        return shortdesc;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getCacheId() {
        if (StringUtils.isBlank(cacheId) && getConnector().equals(GCConnector.getInstance())) {
            return CryptUtils.convertToGcBase31(geocode);
        }

        return cacheId;
    }

    @Override
    public String getGuid() {
        return guid;
    }

    @Override
    public String getLocation() {
        return location;
    }

    @Override
    public String getPersonalNote() {
        // non premium members have no personal notes, premium members have an empty string by default.
        // map both to null, so other code doesn't need to differentiate
        if (StringUtils.isBlank(personalNote)) {
            return null;
        }
        return personalNote;
    }

    public boolean supportsUserActions() {
        return getConnector().supportsUserActions();
    }

    public boolean supportsCachesAround() {
        return getConnector().supportsCachesAround();
    }

    public void shareCache(Activity fromActivity, Resources res) {
        if (geocode == null) {
            return;
        }

        StringBuilder subject = new StringBuilder("Geocache ");
        subject.append(geocode.toUpperCase());
        if (StringUtils.isNotBlank(name)) {
            subject.append(" - ").append(name);
        }

        final Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, subject.toString());
        intent.putExtra(Intent.EXTRA_TEXT, getUrl());

        fromActivity.startActivity(Intent.createChooser(intent, res.getText(R.string.action_bar_share_title)));
    }

    public String getUrl() {
        return getConnector().getCacheUrl(this);
    }

    public boolean supportsGCVote() {
        return StringUtils.startsWithIgnoreCase(geocode, "GC");
    }

    public void setDescription(final String description) {
        this.description = description;
    }

    @Override
    public boolean isFound() {
        return found;
    }

    @Override
    public boolean isFavorite() {
        return favorite;
    }

    public void setFavorite(boolean favourite) {
        this.favorite = favourite;
    }


    @Override
    public boolean isWatchlist() {
        return onWatchlist;
    }

    @Override
    public Date getHiddenDate() {
        return hidden;
    }

    @Override
    public List<String> getAttributes() {
        if (attributes == null) {
            return Collections.emptyList();
        }
        return Collections.unmodifiableList(attributes);
    }

    @Override
    public List<cgTrackable> getInventory() {
        return inventory;
    }

    @Override
    public ArrayList<cgImage> getSpoilers() {
        return spoilers;
    }

    @Override
    public Map<LogType, Integer> getLogCounts() {
        return logCounts;
    }

    @Override
    public int getFavoritePoints() {
        return favoritePoints;
    }

    @Override
    public String getNameForSorting() {
        if (null == nameForSorting) {
            final Matcher matcher = NUMBER_PATTERN.matcher(name);
            if (matcher.find()) {
                nameForSorting = name.replace(matcher.group(), StringUtils.leftPad(matcher.group(), 6, '0'));
            }
            else {
                nameForSorting = name;
            }
        }
        return nameForSorting;
    }

    public boolean isVirtual() {
        return CacheType.VIRTUAL == cacheType || CacheType.WEBCAM == cacheType
                || CacheType.EARTH == cacheType;
    }

    public boolean showSize() {
        return !((isEventCache() || isVirtual()) && size == CacheSize.NOT_CHOSEN);
    }

    public long getUpdated() {
        return updated;
    }

    public void setUpdated(long updated) {
        this.updated = updated;
    }

    public long getDetailedUpdate() {
        return detailedUpdate;
    }

    public void setDetailedUpdate(long detailedUpdate) {
        this.detailedUpdate = detailedUpdate;
    }

    public long getVisitedDate() {
        return visitedDate;
    }

    public void setVisitedDate(long visitedDate) {
        this.visitedDate = visitedDate;
    }

    public int getListId() {
        return listId;
    }

    public void setListId(int listId) {
        this.listId = listId;
    }

    public boolean isDetailed() {
        return detailed;
    }

    public void setDetailed(boolean detailed) {
        this.detailed = detailed;
    }

    public Spannable getNameSp() {
        return nameSp;
    }

    public void setNameSp(Spannable nameSp) {
        this.nameSp = nameSp;
    }

    public void setHidden(final Date hidden) {
        if (hidden == null) {
            this.hidden = null;
        }
        else {
            this.hidden = new Date(hidden.getTime()); // avoid storing the external reference in this object
        }
    }

    public Float getDirection() {
        return direction;
    }

    public void setDirection(Float direction) {
        this.direction = direction;
    }

    public Float getDistance() {
        return distance;
    }

    public void setDistance(Float distance) {
        this.distance = distance;
    }

    public String getLatlon() {
        return latlon;
    }

    public void setLatlon(String latlon) {
        this.latlon = latlon;
    }

    public Geopoint getCoords() {
        return coords;
    }

    public void setCoords(Geopoint coords) {
        this.coords = coords;
    }

    /**
     * @return true if the coords are from the cache details page and the user has been logged in
     */
    public boolean isReliableLatLon() {
        return reliableLatLon;
    }

    public void setReliableLatLon(boolean reliableLatLon) {
        this.reliableLatLon = reliableLatLon;
    }

    public Double getElevation() {
        return elevation;
    }

    public void setElevation(Double elevation) {
        this.elevation = elevation;
    }

    public String getShortdesc() {
        return shortdesc;
    }

    public void setShortdesc(String shortdesc) {
        this.shortdesc = shortdesc;
    }

    public void setFavoritePoints(int favoriteCnt) {
        this.favoritePoints = favoriteCnt;
    }

    public float getRating() {
        return rating;
    }

    public void setRating(float rating) {
        this.rating = rating;
    }

    public int getVotes() {
        return votes;
    }

    public void setVotes(int votes) {
        this.votes = votes;
    }

    public float getMyVote() {
        return myVote;
    }

    public void setMyVote(float myVote) {
        this.myVote = myVote;
    }

    public int getInventoryItems() {
        return inventoryItems;
    }

    public void setInventoryItems(int inventoryItems) {
        this.inventoryItems = inventoryItems;
    }

    public boolean isOnWatchlist() {
        return onWatchlist;
    }

    public void setOnWatchlist(boolean onWatchlist) {
        this.onWatchlist = onWatchlist;
    }

    /**
     * return an immutable list of waypoints.
     *
     * @return always non <code>null</code>
     */
    public List<cgWaypoint> getWaypoints() {
        if (waypoints == null) {
            return Collections.emptyList();
        }
        return Collections.unmodifiableList(waypoints);
    }

    public void setWaypoints(List<cgWaypoint> waypoints) {
        this.waypoints = waypoints;
        if (waypoints != null) {
            for (cgWaypoint waypoint : waypoints) {
                waypoint.setGeocode(geocode);
            }
        }
    }

    public List<cgLog> getLogs() {
        return getLogs(true);
    }

    /**
     * @param allLogs
     *            true for all logs, false for friend logs only
     * @return the logs with all entries or just the entries of the friends, never <code>null</code>
     */
    public List<cgLog> getLogs(boolean allLogs) {
        if (logs == null) {
            return Collections.emptyList();
        }
        if (allLogs) {
            return logs;
        }
        ArrayList<cgLog> friendLogs = new ArrayList<cgLog>();
        for (cgLog log : logs) {
            if (log.friend) {
                friendLogs.add(log);
            }
        }
        return friendLogs;
    }

    /**
     * @param logs
     *            the log entries
     */
    public void setLogs(List<cgLog> logs) {
        this.logs = logs;
    }

    public boolean isLogOffline() {
        return logOffline;
    }

    public void setLogOffline(boolean logOffline) {
        this.logOffline = logOffline;
    }

    public boolean isStatusChecked() {
        return statusChecked;
    }

    public void setStatusChecked(boolean statusChecked) {
        this.statusChecked = statusChecked;
    }

    public boolean isStatusCheckedView() {
        return statusCheckedView;
    }

    public void setStatusCheckedView(boolean statusCheckedView) {
        this.statusCheckedView = statusCheckedView;
    }

    public String getDirectionImg() {
        return directionImg;
    }

    public void setDirectionImg(String directionImg) {
        this.directionImg = directionImg;
    }

    public void setGeocode(String geocode) {
        this.geocode = geocode;
    }

    public void setCacheId(String cacheId) {
        this.cacheId = cacheId;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public void setOwnerReal(String ownerReal) {
        this.ownerReal = ownerReal;
    }

    public void setHint(String hint) {
        this.hint = hint;
    }

    public void setSize(CacheSize size) {
        this.size = size;
    }

    public void setDifficulty(float difficulty) {
        this.difficulty = difficulty;
    }

    public void setTerrain(float terrain) {
        this.terrain = terrain;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void setPersonalNote(String personalNote) {
        this.personalNote = personalNote;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public void setArchived(boolean archived) {
        this.archived = archived;
    }

    public void setFound(boolean found) {
        this.found = found;
    }

    public void setOwn(boolean own) {
        this.own = own;
    }

    public void setAttributes(List<String> attributes) {
        this.attributes = attributes;
    }

    public void setSpoilers(ArrayList<cgImage> spoilers) {
        this.spoilers = spoilers;
    }

    public void setInventory(List<cgTrackable> inventory) {
        this.inventory = inventory;
    }

    public void setLogCounts(Map<LogType, Integer> logCounts) {
        this.logCounts = logCounts;
    }

    /*
     * (non-Javadoc)
     *
     * @see cgeo.geocaching.IBasicCache#getType()
     *
     * @returns Never null
     */
    @Override
    public CacheType getType() {
        return cacheType;
    }

    public void setType(CacheType cacheType) {
        if (cacheType == null || CacheType.ALL == cacheType) {
            throw new IllegalArgumentException("Illegal cache type");
        }
        this.cacheType = cacheType;
    }

    public boolean hasDifficulty() {
        return difficulty > 0f;
    }

    public boolean hasTerrain() {
        return terrain > 0f;
    }

    /**
     * @return the storageLocation
     */
    public EnumSet<StorageLocation> getStorageLocation() {
        return storageLocation;
    }

    /**
     * @param storageLocation
     *            the storageLocation to set
     */
    public void addStorageLocation(StorageLocation sl) {
        this.storageLocation.add(sl);
    }

    public void addWaypoint(final cgWaypoint waypoint) {
        if (null == waypoints) {
            waypoints = new ArrayList<cgWaypoint>();
        }
        waypoints.add(waypoint);
        waypoint.setGeocode(geocode);
    }

    public boolean hasWaypoints() {
        return CollectionUtils.isNotEmpty(waypoints);
    }

    /**
     * @param index
     * @return <code>true</code>, if the waypoint was duplicated
     */
    public boolean duplicateWaypoint(int index) {
        if (!isValidWaypointIndex(index)) {
            return false;
        }
        final cgWaypoint copy = new cgWaypoint(waypoints.get(index));
        copy.setUserDefined();
        copy.setName(cgeoapplication.getInstance().getString(R.string.waypoint_copy_of) + " " + copy.getName());
        waypoints.add(index + 1, copy);
        return cgeoapplication.getInstance().saveOwnWaypoint(-1, geocode, copy);
    }

    private boolean isValidWaypointIndex(int index) {
        if (!hasWaypoints()) {
            return false;
        }
        if (index < 0 || index >= waypoints.size()) {
            return false;
        }
        return true;
    }

    /**
     * delete a user defined waypoint
     *
     * @param index
     * @return <code>true</code>, if the waypoint was deleted
     */
    public boolean deleteWaypoint(int index) {
        if (!isValidWaypointIndex(index)) {
            return false;
        }
        final cgWaypoint waypoint = waypoints.get(index);
        if (waypoint.isUserDefined()) {
            waypoints.remove(index);
            cgeoapplication.getInstance().deleteWaypoint(waypoint.getId());
            cgeoapplication.removeCacheFromCache(geocode);
            return true;
        }
        return false;
    }

    /**
     * @param index
     * @return waypoint or <code>null</code>
     */
    public cgWaypoint getWaypoint(int index) {
        if (!isValidWaypointIndex(index)) {
            return null;
        }
        return waypoints.get(index);
    }

    public void parseWaypointsFromNote() {
        try {
            if (StringUtils.isBlank(getPersonalNote())) {
                return;
            }
            final Pattern coordPattern = Pattern.compile("\\b[nNsS]{1}\\s*\\d"); // begin of coordinates
            int count = 1;
            String note = getPersonalNote();
            Matcher matcher = coordPattern.matcher(note);
            while (matcher.find()) {
                try {
                    final Geopoint point = GeopointParser.parse(note.substring(matcher.start()));
                    // coords must have non zero latitude and longitude and at least one part shall have fractional degrees
                    if (point != null && point.getLatitudeE6() != 0 && point.getLongitudeE6() != 0 && ((point.getLatitudeE6() % 1000) != 0 || (point.getLongitudeE6() % 1000) != 0)) {
                        final String name = cgeoapplication.getInstance().getString(R.string.cache_personal_note) + " " + count;
                        final cgWaypoint waypoint = new cgWaypoint(name, WaypointType.WAYPOINT);
                        waypoint.setCoords(point);
                        addWaypoint(waypoint);
                        count++;
                    }
                } catch (GeopointParser.ParseException e) {
                    // ignore
                }

                note = note.substring(matcher.start() + 1);
                matcher = coordPattern.matcher(note);
            }
        } catch (Exception e) {
            Log.e(Settings.tag, "cgCache.parseWaypointsFromNote: " + e.toString());
        }
    }

    public void addAttribute(final String attribute) {
        if (attributes == null) {
            attributes = new ArrayList<String>();
        }
        attributes.add(attribute);
    }

    public boolean hasAttributes() {
        return attributes != null && attributes.size() > 0;
    }

    public void prependLog(final cgLog log) {
        if (logs == null) {
            logs = new ArrayList<cgLog>();
        }
        logs.add(0, log);
    }

    public void appendLog(final cgLog log) {
        if (logs == null) {
            logs = new ArrayList<cgLog>();
        }
        logs.add(log);
    }

    /*
     * For working in the debugger
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return this.geocode + " " + this.name;
    }
}