summaryrefslogtreecommitdiffstats
path: root/mailnews/base/content/msgSynchronize.js
blob: 250c8a26c4be1d75a0bcb6e03a84110eb60e3f40 (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
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

Components.utils.import("resource://gre/modules/Services.jsm");

var gSynchronizeTree = null;
var gParentMsgWindow;
var gMsgWindow;

var gInitialFolderStates = {};

function OnLoad()
{
    if (window.arguments && window.arguments[0]) {
        if (window.arguments[0].msgWindow) {
            gParentMsgWindow = window.arguments[0].msgWindow;
        }
    }

    document.getElementById("syncMail").checked =
      Services.prefs.getBoolPref("mailnews.offline_sync_mail");
    document.getElementById("syncNews").checked =
      Services.prefs.getBoolPref("mailnews.offline_sync_news");
    document.getElementById("sendMessage").checked =
      Services.prefs.getBoolPref("mailnews.offline_sync_send_unsent");
    document.getElementById("workOffline").checked =
      Services.prefs.getBoolPref("mailnews.offline_sync_work_offline");

    return true;
}

function syncOkButton()
{

    var syncMail = document.getElementById("syncMail").checked;
    var syncNews = document.getElementById("syncNews").checked;
    var sendMessage = document.getElementById("sendMessage").checked;
    var workOffline = document.getElementById("workOffline").checked;

    Services.prefs.setBoolPref("mailnews.offline_sync_mail", syncMail);
    Services.prefs.setBoolPref("mailnews.offline_sync_news", syncNews);
    Services.prefs.setBoolPref("mailnews.offline_sync_send_unsent", sendMessage);
    Services.prefs.setBoolPref("mailnews.offline_sync_work_offline", workOffline);

    if (syncMail || syncNews || sendMessage || workOffline) {
        var offlineManager = Components.classes["@mozilla.org/messenger/offline-manager;1"]
                                       .getService(Components.interfaces.nsIMsgOfflineManager);
        if(offlineManager)
            offlineManager.synchronizeForOffline(syncNews, syncMail, sendMessage, workOffline, gParentMsgWindow)
    }

    return true;
}

function OnSelect()
{
   top.window.openDialog("chrome://messenger/content/msgSelectOffline.xul", "",
                         "centerscreen,chrome,modal,titlebar,resizable=yes");
   return true;
}

function selectOkButton()
{
    return true;
}

function selectCancelButton()
{
    var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
                        .getService(Components.interfaces.nsIRDFService);
    for (var resourceValue in gInitialFolderStates) {
      var resource = RDF.GetResource(resourceValue);
      var folder = resource.QueryInterface(Components.interfaces.nsIMsgFolder);
      if (gInitialFolderStates[resourceValue])
        folder.setFlag(Components.interfaces.nsMsgFolderFlags.Offline);
      else
        folder.clearFlag(Components.interfaces.nsMsgFolderFlags.Offline);
    }
    return true;
}

function selectOnLoad()
{
    gMsgWindow = Components.classes["@mozilla.org/messenger/msgwindow;1"]
                           .createInstance(Components.interfaces.nsIMsgWindow);
    gMsgWindow.domWindow = window;
    gMsgWindow.rootDocShell.appType = Components.interfaces.nsIDocShell.APP_TYPE_MAIL;

    gSynchronizeTree = document.getElementById('synchronizeTree');

    SortSynchronizePane('folderNameCol', '?folderTreeNameSort');
} 

function SortSynchronizePane(column, sortKey)
{
    var node = FindInWindow(window, column);
    if(!node) {
        dump('Couldnt find sort column\n');
        return;
    }

    node.setAttribute("sort", sortKey);
    node.setAttribute("sortDirection", "natural");
    var col = gSynchronizeTree.columns[column];
    gSynchronizeTree.view.cycleHeader(col);
}

function FindInWindow(currentWindow, id)
{
    var item = currentWindow.document.getElementById(id);
    if(item)
    return item;

    for(var i = 0; i < currentWindow.frames.length; i++) {
        var frameItem = FindInWindow(currentWindow.frames[i], id);
        if(frameItem)
            return frameItem;
    }

    return null;
}


function onSynchronizeClick(event)
{
    // we only care about button 0 (left click) events
    if (event.button != 0)
      return;

    var row = {}
    var col = {}
    var elt = {}

    gSynchronizeTree.treeBoxObject.getCellAt(event.clientX, event.clientY, row, col, elt);
    if (row.value == -1)
      return;

    if (elt.value == "twisty") {
        var folderResource = GetFolderResource(gSynchronizeTree, row.value);
        var folder = folderResource.QueryInterface(Components.interfaces.nsIMsgFolder);

        if (!(gSynchronizeTree.treeBoxObject.view.isContainerOpen(row.value))) {
            var serverType = folder.server.type;
            // imap is the only server type that does folder discovery
            if (serverType != "imap") return;

            if (folder.isServer) {
                var server = folder.server;
                server.performExpand(gMsgWindow);
            }
            else {
                var imapFolder = folderResource.QueryInterface(Components.interfaces.nsIMsgImapMailFolder);
                if (imapFolder) {
                  imapFolder.performExpand(gMsgWindow);
                }
            }
        }
    }
    else {
      if (col.value.id == "syncCol") {   
        UpdateNode(GetFolderResource(gSynchronizeTree, row.value), row.value);
      }
    }
}

function onSynchronizeTreeKeyPress(event)
{
    // for now, only do something on space key
    if (event.charCode != KeyEvent.DOM_VK_SPACE)
      return;

    var treeSelection = gSynchronizeTree.view.selection; 
    for (var i=0;i<treeSelection.getRangeCount();i++) {
      var start = {}, end = {};
      treeSelection.getRangeAt(i,start,end);
      for (var k=start.value;k<=end.value;k++)
        UpdateNode(GetFolderResource(gSynchronizeTree, k), k);
    }
}

function UpdateNode(resource, row)
{
    var folder = resource.QueryInterface(Components.interfaces.nsIMsgFolder);

    if (folder.isServer)
      return;

    if (!(resource.Value in gInitialFolderStates)) {
      gInitialFolderStates[resource.Value] = folder.getFlag(Components.interfaces.nsMsgFolderFlags.Offline);
    }

    folder.toggleFlag(Components.interfaces.nsMsgFolderFlags.Offline);
}

function GetFolderResource(aTree, aIndex) {
  return aTree.builderView.getResourceAtIndex(aIndex);
}