# This patch file was generated by NetBeans IDE
# Following Index: paths are relative to: /home/kos/spark/src/java/org/jivesoftware/sparkimpl/plugin/bookmarks
# This patch can be applied using context Tools: Patch action on respective folder.
# It uses platform neutral UTF-8 encoding and \n newlines.
# Above lines and this line are ignored by the patching process.
Index: BookmarkPlugin.java
--- BookmarkPlugin.java Base (BASE)
+++ BookmarkPlugin.java Locally Modified (Based On LOCAL)
@@ -10,13 +10,10 @@
 
 package org.jivesoftware.sparkimpl.plugin.bookmarks;
 
-import org.jivesoftware.resource.Res;
 import org.jivesoftware.resource.SparkRes;
 import org.jivesoftware.smack.XMPPException;
-import org.jivesoftware.smackx.PrivateDataManager;
 import org.jivesoftware.smackx.bookmark.BookmarkedConference;
 import org.jivesoftware.smackx.bookmark.BookmarkedURL;
-import org.jivesoftware.smackx.bookmark.Bookmarks;
 import org.jivesoftware.spark.SparkManager;
 import org.jivesoftware.spark.plugin.Plugin;
 import org.jivesoftware.spark.ui.conferences.ConferenceUtils;
@@ -34,6 +31,10 @@
 import javax.swing.AbstractAction;
 import javax.swing.Action;
 import javax.swing.JMenu;
+import org.jivesoftware.smackx.bookmark.BookmarkManager;
+import org.jivesoftware.spark.ui.conferences.BookmarksListener;
+import org.jivesoftware.spark.ui.conferences.BookmarksUI;
+import org.jivesoftware.spark.ui.conferences.ConferenceServices;
 
 /**
  * Allows for adding and removal of Bookmarks within Spark.
@@ -43,35 +44,100 @@
     public void initialize() {
         final SwingWorker bookmarkThreadWorker = new SwingWorker() {
             public Object construct() {
-                // Register own provider for simpler implementation.
-                PrivateDataManager.addPrivateDataProvider("storage", "storage:bookmarks", new Bookmarks.Provider());
-                PrivateDataManager manager = new PrivateDataManager(SparkManager.getConnection());
-                Bookmarks bookmarks = null;
-                try {
-                    bookmarks = (Bookmarks)manager.getPrivateData("storage", "storage:bookmarks");
+                return this; 
                 }
-                catch (XMPPException e) {
-                    Log.error(e);
+
+            /**
+             * Installing menu into spark menu and adding events to bookmarks
+             */
+            @Override
+            public void finished() {
+                
+                final JMenu bookmarkMenu = new JMenu("Bookmarks");
+
+                createMenu(bookmarkMenu);
+
+                if (bookmarkMenu.getMenuComponentCount() > 0) {
+                    int menuCount = SparkManager.getMainWindow().getMenu().getMenuCount();
+                    SparkManager.getMainWindow().getMenu().add(bookmarkMenu, menuCount - 1);
                 }
-                return bookmarks;
 
+                BookmarksUI bookmarksUi = ConferenceServices.getBookmarkedConferences();
+                if ( bookmarksUi != null ) {
+                    bookmarksUi.addBookmarksListener(new BookmarksListener() {
+
+                        public void bookmarkAdded(String roomJID) {
+                            rescan(bookmarkMenu);
             }
 
-            public void finished() {
-                final Bookmarks bookmarks = (Bookmarks)get();
+                        public void bookmarkRemoved(String roomJID) {
+                            rescan(bookmarkMenu);
+                        }
+                    });
+                }
+            }
 
-                final JMenu bookmarkMenu = new JMenu("Bookmarks");
+            /**
+             * Rescaning our bookmarks and remaking menu items
+             *
+             * @param Bookmark menu Jmenu
+             */
+            public void rescan(JMenu bookmarkMenu)
+            {
+                bookmarkMenu.removeAll(); // removing old menus
+                try {
+                    setBookmarks(bookmarkMenu); // making new 
+                    int onPanel = SparkManager.getMainWindow().getMenu().getComponentIndex(bookmarkMenu);
 
-                if (bookmarks != null) {
-                    bookmarkMenu.setToolTipText(Res.getString("title.view.bookmarks"));
+                    if (onPanel < 0) {
+                        if (bookmarkMenu.getMenuComponentCount() > 0) {
+                            int menuCount = SparkManager.getMainWindow().getMenu().getMenuCount();
+                            SparkManager.getMainWindow().getMenu().add(bookmarkMenu, menuCount - 1);
+                        }
+                    }
+                    
+                    if (onPanel >= 0) {
+                        if (bookmarkMenu.getMenuComponentCount() <= 0) {
+                            SparkManager.getMainWindow().getMenu().remove(bookmarkMenu);
+                        }
+                    }
+                } catch (XMPPException ex) {
+                    Log.error(ex);
+                }
+            }
+
+            /**
+             * Updating statusbar and generating menu items
+             *
+             * @param Bookmark menu Jmenu
+             */
+            public void createMenu(JMenu bookmarkMenu)
+            {
                     SparkManager.getWorkspace().getStatusBar().invalidate();
                     SparkManager.getWorkspace().getStatusBar().validate();
                     SparkManager.getWorkspace().getStatusBar().repaint();
+                try {
+                    setBookmarks(bookmarkMenu);
+                } catch (XMPPException ex) {
+                    Log.error(ex);
+                }
 
+            }
 
-                    Collection bookmarkedConferences = bookmarks.getBookmarkedConferences();
-                    final Collection bookmarkedLinks = bookmarks.getBookmarkedURLS();
+            /**
+             * loading menu items and setting bookmarks listeners
+             *
+             * @param Bookmark menu Jmenu
+             */
+            public void setBookmarks(JMenu bookmarkMenu ) throws XMPPException
+            {
+                BookmarkManager manager = BookmarkManager.getBookmarkManager(SparkManager.getConnection());
 
+                if (manager != null) {
+
+                    Collection bookmarkedConferences = manager.getBookmarkedConferences();
+                    final Collection bookmarkedLinks = manager.getBookmarkedURLs();
+
                     for (Object bookmarkedLink : bookmarkedLinks) {
                         final BookmarkedURL link = (BookmarkedURL) bookmarkedLink;
 
@@ -112,12 +178,7 @@
                         bookmarkMenu.add(conferenceAction);
                     }
                 }
-
-                if (bookmarkMenu.getMenuComponentCount() > 0) {
-                    int menuCount = SparkManager.getMainWindow().getMenu().getMenuCount();
-                    SparkManager.getMainWindow().getMenu().add(bookmarkMenu, menuCount - 1);
                 }
-            }
         };
 
         bookmarkThreadWorker.start();
