Search in sources :

Example 6 with RoomChat

use of com.xabber.android.data.extension.muc.RoomChat in project xabber-android by redsolution.

the class MessageManager method exportChat.

/**
     * Export chat to file with specified name.
     *
     * @param account
     * @param user
     * @param fileName
     * @throws NetworkException
     */
public File exportChat(String account, String user, String fileName) throws NetworkException {
    final File file = new File(Environment.getExternalStorageDirectory(), fileName);
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        final String titleName = RosterManager.getInstance().getName(account, user) + " (" + user + ")";
        out.write("<html><head><title>");
        out.write(StringUtils.escapeHtml(titleName));
        out.write("</title></head><body>");
        final AbstractChat abstractChat = getChat(account, user);
        if (abstractChat != null) {
            final boolean isMUC = abstractChat instanceof RoomChat;
            final String accountName = AccountManager.getInstance().getNickName(account);
            final String userName = RosterManager.getInstance().getName(account, user);
            for (MessageItem messageItem : abstractChat.getMessages()) {
                if (messageItem.getAction() != null) {
                    continue;
                }
                final String name;
                if (isMUC) {
                    name = messageItem.getResource();
                } else {
                    if (messageItem.isIncoming()) {
                        name = userName;
                    } else {
                        name = accountName;
                    }
                }
                out.write("<b>");
                out.write(StringUtils.escapeHtml(name));
                out.write("</b>&nbsp;(");
                out.write(StringUtils.getDateTimeText(messageItem.getTimestamp()));
                out.write(")<br />\n<p>");
                out.write(StringUtils.escapeHtml(messageItem.getText()));
                out.write("</p><hr />\n");
            }
        }
        out.write("</body></html>");
        out.close();
    } catch (IOException e) {
        throw new NetworkException(R.string.FILE_NOT_FOUND);
    }
    return file;
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) RoomChat(com.xabber.android.data.extension.muc.RoomChat) NetworkException(com.xabber.android.data.NetworkException) BufferedWriter(java.io.BufferedWriter)

Aggregations

RoomChat (com.xabber.android.data.extension.muc.RoomChat)6 AbstractChat (com.xabber.android.data.message.AbstractChat)4 RoomContact (com.xabber.android.data.extension.muc.RoomContact)3 ChatContact (com.xabber.android.data.message.ChatContact)3 ArrayList (java.util.ArrayList)3 AbstractContact (com.xabber.android.data.roster.AbstractContact)2 RosterContact (com.xabber.android.data.roster.RosterContact)2 NetworkException (com.xabber.android.data.NetworkException)1 ChatsShowStatusChange (com.xabber.android.data.SettingsManager.ChatsShowStatusChange)1 CommonState (com.xabber.android.data.account.CommonState)1 BaseEntity (com.xabber.android.data.entity.BaseEntity)1 RoomState (com.xabber.android.data.extension.muc.RoomState)1 RegularChat (com.xabber.android.data.message.RegularChat)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Map (java.util.Map)1