~berthold-daum/zora/trunk

« back to all changes in this revision

Viewing changes to com.bdaum.zoom.operations/src/com/bdaum/zoom/operations/internal/DeleteVoicenoteOperation.java

  • Committer: bdaum
  • Date: 2015-12-26 10:21:51 UTC
  • Revision ID: berthold.daum@bdaum.de-20151226102151-44f1j5113167thb9
VersionĀ 2.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of the ZoRa project: http://www.photozora.org.
3
 
 *
4
 
 * ZoRa is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * ZoRa is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with ZoRa; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 *
18
 
 * (c) 2009 Berthold Daum  (berthold.daum@bdaum.de)
19
 
 */
20
 
 
21
 
package com.bdaum.zoom.operations.internal;
22
 
 
23
 
import java.io.File;
24
 
import java.net.URI;
25
 
import java.util.ArrayList;
26
 
import java.util.Date;
27
 
import java.util.HashSet;
28
 
import java.util.List;
29
 
import java.util.Set;
30
 
 
31
 
import org.eclipse.core.commands.ExecutionException;
32
 
import org.eclipse.core.runtime.IAdaptable;
33
 
import org.eclipse.core.runtime.IProgressMonitor;
34
 
import org.eclipse.core.runtime.IStatus;
35
 
import org.eclipse.core.runtime.jobs.Job;
36
 
import org.eclipse.osgi.util.NLS;
37
 
 
38
 
import com.bdaum.zoom.cat.model.asset.Asset;
39
 
import com.bdaum.zoom.core.Constants;
40
 
import com.bdaum.zoom.core.Core;
41
 
import com.bdaum.zoom.core.IVolumeManager;
42
 
import com.bdaum.zoom.core.db.IDbErrorHandler;
43
 
import com.bdaum.zoom.operations.DbOperation;
44
 
import com.bdaum.zoom.operations.IProfiledOperation;
45
 
 
46
 
public class DeleteVoicenoteOperation extends DbOperation {
47
 
 
48
 
        private List<Asset> assets;
49
 
 
50
 
        public DeleteVoicenoteOperation(List<Asset> assets) {
51
 
                super(Messages.getString("DeleteVoicenoteOperation.Delete_voice_notes")); //$NON-NLS-1$
52
 
                this.assets = assets;
53
 
                this.date = new Date();
54
 
        }
55
 
 
56
 
        @Override
57
 
        public IStatus execute(IProgressMonitor aMonitor, final IAdaptable info)
58
 
                        throws ExecutionException {
59
 
                final Set<String> volumes = new HashSet<String>();
60
 
                final List<String> errands = new ArrayList<String>();
61
 
                init(aMonitor, assets.size() + 2);
62
 
                boolean deleted = false;
63
 
                IVolumeManager volumeManager = Core.getCore().getVolumeManager();
64
 
                for (Asset asset : assets) {
65
 
                        if (asset.getFileState() != IVolumeManager.PEER) {
66
 
                                if (".".equals(asset.getVoiceFileURI())) { //$NON-NLS-1$
67
 
                                        URI uri = volumeManager.findFile(asset);
68
 
                                        if (uri != null) {
69
 
                                                if (volumeManager.findExistingFile(asset, true) != null) {
70
 
                                                        deleted |= deleteVoiceNote(asset);
71
 
                                                } else {
72
 
                                                        String volume = asset.getVolume();
73
 
                                                        if (volume != null && volume.length() > 0)
74
 
                                                                volumes.add(volume);
75
 
                                                        errands.add(uri.toString());
76
 
                                                }
77
 
                                        }
78
 
                                } else
79
 
                                        deleted |= deleteVoiceNote(asset);
80
 
                                if (aMonitor.isCanceled())
81
 
                                        return abort();
82
 
                        }
83
 
                }
84
 
                if (deleted)
85
 
                        fireAssetsModified(assets, null);
86
 
                if (errands.size() > 0) {
87
 
                        final IDbErrorHandler errorHandler = Core.getCore()
88
 
                                        .getErrorHandler();
89
 
                        if (errorHandler != null) {
90
 
                                String msg;
91
 
                                if (errands.size() == 1) {
92
 
                                        msg = NLS
93
 
                                                        .bind(Messages
94
 
                                                                        .getString("DeleteVoicenoteOperation.File_offline"), //$NON-NLS-1$
95
 
                                                                        errands.get(0), volumes.toArray()[0]);
96
 
                                } else {
97
 
                                        StringBuffer sb = new StringBuffer();
98
 
                                        for (String volume : volumes) {
99
 
                                                if (sb.length() > 0)
100
 
                                                        sb.append(", "); //$NON-NLS-1$
101
 
                                                sb.append(volume);
102
 
                                        }
103
 
                                        msg = NLS
104
 
                                                        .bind(Messages
105
 
                                                                        .getString("DeleteVoicenoteOperation.Files_offline"), //$NON-NLS-1$
106
 
                                                                        errands.size(), sb.toString());
107
 
 
108
 
                                }
109
 
                                errorHandler
110
 
                                                .showInformation(
111
 
                                                                Messages.getString("DeleteVoicenoteOperation.Unable_to_delete"), //$NON-NLS-1$
112
 
                                                                msg, info);
113
 
                        }
114
 
                }
115
 
                assets = null;
116
 
                return close(info);
117
 
        }
118
 
 
119
 
        private boolean deleteVoiceNote(Asset asset) {
120
 
                String voiceFileURI = asset.getVoiceFileURI();
121
 
                if (voiceFileURI != null && voiceFileURI.length() > 0) {
122
 
                        if (".".equals(voiceFileURI)) { //$NON-NLS-1$
123
 
                                IVolumeManager volumeManager = Core.getCore()
124
 
                                                .getVolumeManager();
125
 
                                URI uri = volumeManager.findVoiceFile(asset);
126
 
                                if (uri != null && Constants.FILESCHEME.equals(uri.getScheme()))
127
 
                                        new File(uri).delete();
128
 
                        }
129
 
                        asset.setVoiceFileURI(null);
130
 
                        storeSafely(null, 1, asset);
131
 
                        return true;
132
 
                }
133
 
                return false;
134
 
        }
135
 
 
136
 
        @Override
137
 
        public boolean canRedo() {
138
 
                return false;
139
 
        }
140
 
 
141
 
        @Override
142
 
        public IStatus redo(IProgressMonitor aMonitor, IAdaptable info)
143
 
                        throws ExecutionException {
144
 
                return null;
145
 
        }
146
 
 
147
 
        @Override
148
 
        public boolean canUndo() {
149
 
                return false;
150
 
        }
151
 
 
152
 
        @Override
153
 
        public IStatus undo(IProgressMonitor aMonitor, IAdaptable info)
154
 
                        throws ExecutionException {
155
 
                return null;
156
 
        }
157
 
 
158
 
        public int getExecuteProfile() {
159
 
                return IProfiledOperation.CONTENT;
160
 
        }
161
 
 
162
 
        public int getUndoProfile() {
163
 
                return 0;
164
 
        }
165
 
 
166
 
        @Override
167
 
        public int getPriority() {
168
 
                return assets.size() > 3 ? Job.LONG : Job.SHORT;
169
 
        }
170
 
 
171
 
}