| 1 | /*- |
|---|
| 2 | * Copyright (c) 2010, Derek Konigsberg |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * |
|---|
| 9 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 10 | * notice, this list of conditions and the following disclaimer. |
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | * documentation and/or other materials provided with the distribution. |
|---|
| 14 | * 3. Neither the name of the project nor the names of its |
|---|
| 15 | * contributors may be used to endorse or promote products derived |
|---|
| 16 | * from this software without specific prior written permission. |
|---|
| 17 | * |
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|---|
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|---|
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 30 | */ |
|---|
| 31 | package org.logicprobe.LogicMail; |
|---|
| 32 | |
|---|
| 33 | import java.io.EOFException; |
|---|
| 34 | |
|---|
| 35 | import org.logicprobe.LogicMail.conf.AccountConfig; |
|---|
| 36 | import org.logicprobe.LogicMail.conf.MailSettings; |
|---|
| 37 | import org.logicprobe.LogicMail.util.DataStore; |
|---|
| 38 | import org.logicprobe.LogicMail.util.DataStoreFactory; |
|---|
| 39 | import org.logicprobe.LogicMail.util.DataStoreSyncObject; |
|---|
| 40 | |
|---|
| 41 | import net.rim.device.api.i18n.Locale; |
|---|
| 42 | import net.rim.device.api.notification.NotificationsConstants; |
|---|
| 43 | import net.rim.device.api.notification.NotificationsManager; |
|---|
| 44 | import net.rim.device.api.synchronization.SyncCollection; |
|---|
| 45 | import net.rim.device.api.synchronization.SyncConverter; |
|---|
| 46 | import net.rim.device.api.synchronization.SyncObject; |
|---|
| 47 | import net.rim.device.api.util.DataBuffer; |
|---|
| 48 | |
|---|
| 49 | public class LogicMailSyncCollection implements SyncCollection, SyncConverter { |
|---|
| 50 | private static LogicMailSyncCollection instance; |
|---|
| 51 | private DataStore[] dataStores; |
|---|
| 52 | |
|---|
| 53 | private LogicMailSyncCollection() { |
|---|
| 54 | dataStores = new DataStore[3]; |
|---|
| 55 | dataStores[0] = DataStoreFactory.getConfigurationStore(); |
|---|
| 56 | dataStores[1] = DataStoreFactory.getConnectionCacheStore(); |
|---|
| 57 | dataStores[2] = DataStoreFactory.getMetadataStore(); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | public static synchronized LogicMailSyncCollection getInstance() { |
|---|
| 61 | if(instance == null) { |
|---|
| 62 | instance = new LogicMailSyncCollection(); |
|---|
| 63 | } |
|---|
| 64 | return instance; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public void beginTransaction() { |
|---|
| 68 | // Nothing needs to be done here |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | public void endTransaction() { |
|---|
| 72 | // Commit is done during add |
|---|
| 73 | |
|---|
| 74 | // Re-run all the startup code, which should be refactored into a |
|---|
| 75 | // common place to avoid duplication |
|---|
| 76 | LogicMailRuntimeState runtimeState = LogicMailRuntimeState.getInstance(); |
|---|
| 77 | runtimeState.removeAllEventSources(); |
|---|
| 78 | |
|---|
| 79 | MailSettings mailSettings = MailSettings.getInstance(); |
|---|
| 80 | mailSettings.reloadSettings(); |
|---|
| 81 | |
|---|
| 82 | int numAccounts = mailSettings.getNumAccounts(); |
|---|
| 83 | for(int i=0; i<numAccounts; i++) { |
|---|
| 84 | AccountConfig accountConfig = mailSettings.getAccountConfig(i); |
|---|
| 85 | LogicMailEventSource eventSource = |
|---|
| 86 | new LogicMailEventSource(accountConfig.getAcctName(), accountConfig.getUniqueId()); |
|---|
| 87 | NotificationsManager.registerSource( |
|---|
| 88 | eventSource.getEventSourceId(), |
|---|
| 89 | eventSource, |
|---|
| 90 | NotificationsConstants.CASUAL); |
|---|
| 91 | runtimeState.putEventSource(eventSource); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | public SyncConverter getSyncConverter() { |
|---|
| 96 | return this; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public String getSyncName() { |
|---|
| 100 | return "LogicMail"; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | public String getSyncName(Locale locale) { |
|---|
| 104 | return null; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | public int getSyncObjectCount() { |
|---|
| 108 | return dataStores.length; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | public SyncObject[] getSyncObjects() { |
|---|
| 112 | SyncObject[] result = new SyncObject[dataStores.length]; |
|---|
| 113 | for(int i=0; i<dataStores.length; i++) { |
|---|
| 114 | result[i] = dataStores[i].getSyncObject(); |
|---|
| 115 | } |
|---|
| 116 | return result; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | public SyncObject getSyncObject(int uid) { |
|---|
| 120 | for(int i=0; i<dataStores.length; i++) { |
|---|
| 121 | if(dataStores[i].getSyncObjectUID() == uid) { |
|---|
| 122 | return dataStores[i].getSyncObject(); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | return null; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | public int getSyncVersion() { |
|---|
| 129 | return 1; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | public boolean addSyncObject(SyncObject object) { |
|---|
| 133 | if(!(object instanceof DataStoreSyncObject)) { return false; } |
|---|
| 134 | |
|---|
| 135 | for(int i=0; i<dataStores.length; i++) { |
|---|
| 136 | if(dataStores[i].getSyncObjectUID() == object.getUID()) { |
|---|
| 137 | return dataStores[i].setSyncObject((DataStoreSyncObject)object); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | return false; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | public boolean removeSyncObject(SyncObject object) { |
|---|
| 145 | return false; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | public boolean removeAllSyncObjects() { |
|---|
| 149 | return false; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | public void clearSyncObjectDirty(SyncObject object) { |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | public void setSyncObjectDirty(SyncObject object) { |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | public boolean isSyncObjectDirty(SyncObject object) { |
|---|
| 159 | return false; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | public boolean updateSyncObject(SyncObject oldObject, SyncObject newObject) { |
|---|
| 163 | return false; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | public SyncObject convert(DataBuffer data, int version, int UID) { |
|---|
| 167 | // Reading in a synchronization object |
|---|
| 168 | |
|---|
| 169 | if(version != getSyncVersion()) { return null; } |
|---|
| 170 | |
|---|
| 171 | try { |
|---|
| 172 | DataStoreSyncObject syncObject = new DataStoreSyncObject(UID); |
|---|
| 173 | syncObject.load(data); |
|---|
| 174 | return syncObject; |
|---|
| 175 | } catch (EOFException e) { |
|---|
| 176 | return null; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | public boolean convert(SyncObject object, DataBuffer buffer, int version) { |
|---|
| 181 | // Writing out a synchronization object |
|---|
| 182 | |
|---|
| 183 | if(version != getSyncVersion()) { return false; } |
|---|
| 184 | |
|---|
| 185 | if(object instanceof DataStoreSyncObject) { |
|---|
| 186 | DataStoreSyncObject syncObject = (DataStoreSyncObject)object; |
|---|
| 187 | syncObject.save(buffer); |
|---|
| 188 | return true; |
|---|
| 189 | } |
|---|
| 190 | else { |
|---|
| 191 | return false; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | } |
|---|