diff options
3 files changed, 146 insertions, 14 deletions
diff --git a/src/net/java/sip/communicator/impl/history/HistoryServiceImpl.java b/src/net/java/sip/communicator/impl/history/HistoryServiceImpl.java index 38b0523..9008c96 100644 --- a/src/net/java/sip/communicator/impl/history/HistoryServiceImpl.java +++ b/src/net/java/sip/communicator/impl/history/HistoryServiceImpl.java @@ -240,10 +240,10 @@ public class HistoryServiceImpl dirs[0] = userSetDataDirectory; else dirs[0] = DATA_DIRECTORY; - + // escape chars in direcotory names escapeCharacters(idComponents); - + System.arraycopy(idComponents, 0, dirs, 1, dirs.length - 1); File directory = null; @@ -255,7 +255,7 @@ public class HistoryServiceImpl throw (IOException) new IOException( "Could not create history due to file system error") .initCause(e); - } + } if (!directory.exists() && !directory.mkdirs()) { @@ -316,12 +316,12 @@ public class HistoryServiceImpl } dir.delete(); } - + /** * Replacing the characters that we must escape * used for the created filename. - * - * @param ids Ids - folder names as we are using + * + * @param ids Ids - folder names as we are using * FileSystem for storing files. */ private void escapeCharacters(String[] ids) @@ -329,7 +329,7 @@ public class HistoryServiceImpl for (int i = 0; i < ids.length; i++) { String currId = ids[i]; - + for (int j = 0; j < ESCAPE_SEQUENCES.length; j++) { currId = currId. @@ -358,4 +358,87 @@ public class HistoryServiceImpl return (serviceReference == null) ? null : (FileAccessService) bundleContext.getService(serviceReference); } + + /** + * Moves the content of oldId history to the content of the newId. + * Moves the content from the oldId folder to the newId folder. + * Old folder must exist. + * + * @param oldId old and existing history + * @param newId the place where content of oldId will be moved + * @throws java.io.IOException problem moving to newId + */ + public void moveHistory(HistoryID oldId, HistoryID newId) + throws IOException + { + if(!isHistoryCreated(oldId))// || !isHistoryExisting(newId)) + return; + + File oldDir = this.createHistoryDirectories(oldId); + File newDir = getDirForHistory(newId); + + // make sure parent path is existing + newDir.getParentFile().mkdirs(); + + if(!oldDir.renameTo(newDir)) + { + logger.info("Cannot move history!"); + throw new IOException("Cannot move history!"); + } + + histories.remove(oldId); + } + + /** + * Returns the folder for the given history without creating it. + * @param id the history + * @return the folder for the history + */ + private File getDirForHistory(HistoryID id) + { + // put together subfolder names. + String[] dirNames = id.getID(); + StringBuffer dirName = new StringBuffer(); + for (int i = 0; i < dirNames.length; i++) + { + if (i > 0) + { + dirName.append(File.separatorChar); + } + dirName.append(dirNames[i]); + } + + // get the parent directory + File histDir = null; + try + { + String userSetDataDirectory = + System.getProperty("HistoryServiceDirectory"); + + if(userSetDataDirectory != null) + histDir = this.fileAccessService + .getPrivatePersistentDirectory(userSetDataDirectory); + else + histDir = this.fileAccessService + .getPrivatePersistentDirectory(DATA_DIRECTORY); + + + } catch (Exception e) + { + logger.error("Error opening directory", e); + } + + return new File(histDir, dirName.toString()); + } + + /** + * Checks whether a history is created and stored. + * Exists in the file system. + * @param id the history to check + * @return whether a history is created and stored. + */ + public boolean isHistoryCreated(HistoryID id) + { + return getDirForHistory(id).exists(); + } } diff --git a/src/net/java/sip/communicator/impl/msghistory/MessageHistoryServiceImpl.java b/src/net/java/sip/communicator/impl/msghistory/MessageHistoryServiceImpl.java index d27c811..82cf9c7 100644 --- a/src/net/java/sip/communicator/impl/msghistory/MessageHistoryServiceImpl.java +++ b/src/net/java/sip/communicator/impl/msghistory/MessageHistoryServiceImpl.java @@ -414,17 +414,49 @@ public class MessageHistoryServiceImpl String remoteId = remoteContact == null ? "default" : remoteContact .getAddress(); + String account = + remoteContact.getProtocolProvider().getAccountID().getAccountUniqueID(); + HistoryID historyId = HistoryID.createFromRawID( new String[] { "messages", localId, + account, + remoteId }); + + // if this history doesn't exists check to see if old one still exists + // old one is not storing history per account + // if old one exists its converted/moved to the new one + // the new one is in format messages/profile_name/account_uid/contact + // the old one was messages/profile_name/contact + if(!this.historyService.isHistoryCreated(historyId)) + { + HistoryID historyId_old = HistoryID.createFromRawID( + new String[] { "messages", + localId, remoteId }); + if(this.historyService.isHistoryCreated(historyId_old)) + { + try + { + this.historyService.moveHistory(historyId_old, historyId); + } + catch (IOException iOException) + { + // something is wrong just use the old one + historyId = historyId_old; + } + } + } + if (this.historyService.isHistoryExisting(historyId)) { retVal = this.historyService.getHistory(historyId); - } else { + } + else + { retVal = this.historyService.createHistory(historyId, - recordStructure); + recordStructure); } return retVal; diff --git a/src/net/java/sip/communicator/service/history/HistoryService.java b/src/net/java/sip/communicator/service/history/HistoryService.java index 7ca68e0..2bbcd65 100644 --- a/src/net/java/sip/communicator/service/history/HistoryService.java +++ b/src/net/java/sip/communicator/service/history/HistoryService.java @@ -35,7 +35,7 @@ public interface HistoryService { * * @return An iterator to a list of IDs. */ - Iterator<HistoryID> getExistingIDs(); + public Iterator<HistoryID> getExistingIDs(); /** * Returns the history associated with this ID. @@ -46,16 +46,16 @@ public interface HistoryService { * @throws IllegalArgumentException * Thrown if there is no such history. */ - History getHistory(HistoryID id) throws IllegalArgumentException; + public History getHistory(HistoryID id) throws IllegalArgumentException; /** - * Tests if a history with the given ID exists. + * Tests if a history with the given ID exists and is loaded. * * @param id * The ID to test. * @return True if a history with this ID exists. False otherwise. */ - boolean isHistoryExisting(HistoryID id); + public boolean isHistoryExisting(HistoryID id); /** * Creates a new history for this ID. @@ -70,7 +70,7 @@ public interface HistoryService { * @throws IOException * Thrown if the history could not be created due to a IO error. */ - History createHistory(HistoryID id, HistoryRecordStructure recordStructure) + public History createHistory(HistoryID id, HistoryRecordStructure recordStructure) throws IllegalArgumentException, IOException; /** @@ -81,4 +81,21 @@ public interface HistoryService { * Thrown if the history could not be removed due to a IO error. */ public void purgeLocallyStoredHistory(HistoryID id) throws IOException; + + /** + * Moves the content of oldId history to the content of the newId. + * + * @param oldId id of the old and existing history + * @param newId the place where content of oldId will be moved + * @throws java.io.IOException problem moving content to newId. + */ + public void moveHistory(HistoryID oldId, HistoryID newId) + throws IOException; + + /** + * Checks whether a history is created and stored. + * @param id the history to check + * @return whether a history is created and stored. + */ + public boolean isHistoryCreated(HistoryID id); } |