[Pkg-mozext-commits] [tabmixplus] 20/44: Add new function to bookmark sessions with groups

David Prévot taffit at moszumanska.debian.org
Fri Mar 18 20:57:15 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository tabmixplus.

commit a21778d11c487ca37df6ba4b8326fa670896a2ad
Author: onemen <tabmix.onemen at gmail.com>
Date:   Sun Feb 28 23:18:29 2016 +0200

    Add new function to bookmark sessions with groups
---
 chrome/content/session/session.js              | 26 +++++++
 chrome/content/tabmix.xul                      |  3 +
 chrome/locale/en-US/session-manager.properties |  1 +
 chrome/locale/en-US/tabmix.dtd                 |  2 +
 modules/TabGroupsMigrator.jsm                  | 94 ++++++++++++++++++++++++++
 5 files changed, 126 insertions(+)

diff --git a/chrome/content/session/session.js b/chrome/content/session/session.js
index b4aa8cd..d0c937d 100644
--- a/chrome/content/session/session.js
+++ b/chrome/content/session/session.js
@@ -1632,6 +1632,13 @@ TabmixSessionManager = {
     else
       document.getElementById("tm-sm-Rename").removeAttribute("disabled");
 
+    if (Tabmix.isVersion(450)) {
+      let bookmark = document.getElementById("tm-sm-bookmark");
+      triggerNode.state = TabmixConvertSession.getSessionState(triggerNode.session, true);
+      triggerNode.groupData = this.TabmixGroupsMigrator.gatherGroupData(triggerNode.state);
+      bookmark.hidden = !triggerNode.groupData.exist;
+    }
+
     var node = triggerNode.parentNode.parentNode;
     var mItem = document.getElementById("tm-sm-SetAsStartup");
     if (node.hasAttribute("sessionmanager-menu")) {
@@ -1725,6 +1732,25 @@ TabmixSessionManager = {
     }
   },
 
+  bookmarkAllGroups: function(node) {
+    let {groupData, session} = node;
+    let name = this.getDecodedLiteralValue(session, "name");
+    if (groupData && groupData.exist) {
+      let oldGuid = this.getLiteralValue("rdf:backupSessionWithGroups", "guid");
+      let promise = this.TabmixGroupsMigrator.bookmarkAllGroupsFromState(groupData.data, oldGuid, name);
+      this.saveGuid(promise, oldGuid);
+    }
+  },
+
+  saveGuid: function(promise, oldGuid) {
+    promise.then(({guid}) => {
+      if (oldGuid != guid) {
+        this.setLiteral("rdf:backupSessionWithGroups", "guid", guid);
+        this.saveStateDelayed();
+      }
+    });
+  },
+
   removeFromMenu: function(event, popup, root) {
     if (!Tabmix.prefs.getBoolPref("middleclickDelete")) return;
     if (event.button == 1 && ("session" in event.target)) {
diff --git a/chrome/content/tabmix.xul b/chrome/content/tabmix.xul
index d2f6b10..8226f9b 100644
--- a/chrome/content/tabmix.xul
+++ b/chrome/content/tabmix.xul
@@ -347,6 +347,9 @@
                   oncommand="TabmixSessionManager.saveClosedSession(this.parentNode.triggerNode);"/>
         <menuitem id="tm-sm-Rename" label="&sm.context.rename;" accesskey="&sm.context.rename.key;"
                   oncommand="TabmixSessionManager.renameSession(this.parentNode.triggerNode.session);"/>
+        <menuitem id="tm-sm-bookmark" label="&sm.context.bookmarkgroups;"
+                  accesskey="&sm.context.bookmarkgroups.key;" hidden="true"
+                  oncommand="TabmixSessionManager.bookmarkAllGroups(this.parentNode.triggerNode);"/>
         <menuseparator/>
         <menuitem id="tm-sm-Delete" label="&sm.context.delete;" accesskey="&sm.context.delete.key;"
                   oncommand="TabmixSessionManager.removeSavedSession(this.parentNode.triggerNode);"/>
diff --git a/chrome/locale/en-US/session-manager.properties b/chrome/locale/en-US/session-manager.properties
index 0dbf4a5..7d76ff8 100644
--- a/chrome/locale/en-US/session-manager.properties
+++ b/chrome/locale/en-US/session-manager.properties
@@ -114,3 +114,4 @@ sm.tabview.install.accesskey=I
 sm.tabview.backup.session=History Session, backup with groups data
 sm.tabview.backup.crashed=Crashed Session, backup with groups data
 sm.tabview.backup.msg=Tab mix plus Session Manager has saved a backup of your session.
+sm.bookmarks.sessionFolder=Tab Mix Plus Sessions
diff --git a/chrome/locale/en-US/tabmix.dtd b/chrome/locale/en-US/tabmix.dtd
index 6867a27..c93b481 100644
--- a/chrome/locale/en-US/tabmix.dtd
+++ b/chrome/locale/en-US/tabmix.dtd
@@ -102,6 +102,8 @@
 <!ENTITY sm.context.save.key "S">
 <!ENTITY sm.context.rename "Rename">
 <!ENTITY sm.context.rename.key "R">
+<!ENTITY sm.context.bookmarkgroups "Bookmark Groups">
+<!ENTITY sm.context.bookmarkgroups.key "B">
 <!ENTITY sm.context.delete "Delete">
 <!ENTITY sm.context.delete.key "D">
 <!ENTITY sm.context.deleteall "Delete All">
diff --git a/modules/TabGroupsMigrator.jsm b/modules/TabGroupsMigrator.jsm
index f7d6e42..68fde72 100644
--- a/modules/TabGroupsMigrator.jsm
+++ b/modules/TabGroupsMigrator.jsm
@@ -1,3 +1,4 @@
+/* globals AsyncShutdown */
 "use strict";
 
 this.EXPORTED_SYMBOLS = ["TabmixGroupsMigrator"];
@@ -9,6 +10,9 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Services",
                                   "resource://gre/modules/Services.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "Promise",
+                                  "resource://gre/modules/Promise.jsm");
+
 XPCOMUtils.defineLazyGetter(this, "TabGroupsMigrator", function() {
   if (!TabmixSvc.version(470)) {
     return null;
@@ -25,9 +29,18 @@ XPCOMUtils.defineLazyGetter(this, "TabGroupsMigrator", function() {
   return tmp.TabGroupsMigrator;
 });
 
+XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
+                                  "resource://gre/modules/PlacesUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown",
+                                  "resource://gre/modules/AsyncShutdown.jsm");
+
 XPCOMUtils.defineLazyModuleGetter(this, "TabmixSvc",
                                   "resource://tabmixplus/Services.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "TabmixPlacesUtils",
+                                  "resource://tabmixplus/Places.jsm");
+
 this.TabmixGroupsMigrator = {
   /**
    * If previous or last session contains tab groups, save a back
@@ -167,4 +180,85 @@ this.TabmixGroupsMigrator = {
     let hiddenTabState = TabGroupsMigrator._removeHiddenTabGroupsFromState(state, groupData);
     return hiddenTabState;
   },
+
+  isGroupExist: function(groupData) {
+    return [...groupData.keys()].length > 0;
+  },
+
+  gatherGroupData: function(state) {
+    let data;
+    if (TabGroupsMigrator) {
+      data = TabGroupsMigrator._gatherGroupData(state);
+    }
+    return {
+      exist: this.isGroupExist(data || new Map()),
+      data: data
+    };
+  },
+
+  setTabTitle: function(groupData) {
+    for (let [, windowGroupMap] of groupData) {
+      let windowGroups = [... windowGroupMap.values()];
+      for (let group of windowGroups) {
+        for (let tab of group.tabs) {
+          let entry = tab.entries[tab.index - 1];
+          let title = tab.title || entry.title;
+          tab.title = TabmixPlacesUtils.getTitleFromBookmark(entry.url, title);
+        }
+      }
+    }
+  },
+
+  promiseItemId: function({guid}) {
+    return PlacesUtils.promiseItemId(guid).then(id => {
+      return {id: id, guid: guid};
+    });
+  },
+
+  createtSessionsFolder: function() {
+    let BM = PlacesUtils.bookmarks;
+    return BM.insert({
+      parentGuid: BM.menuGuid,
+      type: BM.TYPE_FOLDER,
+      index: 0,
+      title: TabmixSvc.getSMString("sm.bookmarks.sessionFolder"),
+    }).then(this.promiseItemId)
+      .catch(TabmixSvc.console.reportError);
+  },
+
+  getSessionsFolder: function(folder) {
+    return this.promiseItemId(folder).catch(this.createtSessionsFolder.bind(this));
+  },
+
+  bookmarkAllGroupsFromState: function(groupData, guid, name) {
+    let folder = {guid: guid};
+    if (!TabGroupsMigrator || !this.isGroupExist(groupData)) {
+      return Promise.resolve(folder);
+    }
+
+    // replaced title for previously bookmarked tabs with title of the bookmark
+    this.setTabTitle(groupData);
+    let bookmarksFinishedPromise = TabGroupsMigrator._bookmarkAllGroupsFromState(groupData);
+    let promise = bookmarksFinishedPromise;
+
+    let sessionsFolder;
+    if (name) {
+      // move the folder created by TabGroupsMigrator to our folder and replace
+      // its title with the session title
+      let groupsPromise = TabGroupsMigrator.bookmarkedGroupsPromise.then(this.promiseItemId);
+      sessionsFolder = this.getSessionsFolder(folder);
+      let movePromise = Promise.all([groupsPromise, sessionsFolder]).then(([item, parent]) => {
+        let BM = PlacesUtils.bookmarks;
+        BM.moveItem(item.id, parent.id, BM.DEFAULT_INDEX);
+        BM.setItemTitle(item.id, name);
+      });
+      promise = Promise.all([bookmarksFinishedPromise, movePromise]);
+    }
+
+    AsyncShutdown.profileBeforeChange.addBlocker(
+      "Tab groups migration bookmarks",
+      promise
+    );
+    return sessionsFolder;
+  },
 };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/tabmixplus.git



More information about the Pkg-mozext-commits mailing list