Subversion Repositories eFlore/Archives.eflore-xul

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 jpm 1
/*
2
Copyright (C) 2003 Gregoire Lejeune <gregoire.lejeune@free.fr>
3
 
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
8
 
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
 
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
17
*/
18
 
19
/**
20
Desc : Script permettant d'ajouter une fenetre dans la SideBar.
21
Usage:
22
  ...
23
  <script type="application/x-javascript" src="chrome://zoolmark/content/addpanel.js"/>
24
  ...
25
  <script type="application/x-javascript">
26
    addPanel( "chrome://zoolmark/content/zoolmarkBrowser.xul" );
27
  </script>
28
  ...
29
*/
30
 
31
const DEBUG = false; /* set to false to suppress debug messages */
32
// const PANELS_RDF_FILE  = "UPnls"; /* directory services property to find panels.rdf */
33
 
34
// const PANELS_RDF_FILE  = "UPnls"; /* directory services property to find panels.rdf */
35
const SIDEBAR_CID      = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
36
const SIDEBAR_CONTRACTID   = "@mozilla.org/sidebar;1";
37
const CONTAINER_CONTRACTID = "@mozilla.org/rdf/container;1";
38
const DIR_SERV_CONTRACTID  = "@mozilla.org/file/directory_service;1"
39
const STD_URL_CONTRACTID   = "@mozilla.org/network/standard-url;1"
40
const NETSEARCH_CONTRACTID = "@mozilla.org/rdf/datasource;1?name=internetsearch"
41
const nsIRDFContainer  = Components.interfaces.nsIRDFContainer;
42
const nsIProperties    = Components.interfaces.nsIProperties;
43
const nsIFileURL       = Components.interfaces.nsIFileURL;
44
const nsIRDFRemoteDataSource = Components.interfaces.nsIRDFRemoteDataSource;
45
 
46
function addPanel( winurl, winname )
47
{
48
  debug("addPanel() - start");
49
  var linksSidebar = new mySidebar();
50
  linksSidebar.addPanel( winurl, winname );
51
  debug("addPanel() - end");
52
}
53
 
54
function mySidebar()
55
{
56
  const RDF_CONTRACTID = "@mozilla.org/rdf/rdf-service;1";
57
  const nsIRDFService = Components.interfaces.nsIRDFService;
58
 
59
  this.rdf = Components.classes[RDF_CONTRACTID].getService(nsIRDFService);
60
  debug('rdf is ' + this.rdf);
61
  this.datasource_uri = getSidebarDatasourceURI(PANELS_RDF_FILE);
62
  debug('datasource_uri is ' + this.datasource_uri + " :: " + PANELS_RDF_FILE + " :: " + SIDEBAR_CID);
63
  this.resource = 'urn:sidebar:current-panel-list';
64
  this.datasource = this.rdf.GetDataSource(this.datasource_uri);
65
}
66
 
67
mySidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
68
 
69
/* decorate prototype to provide ``class'' methods and property accessors */
70
mySidebar.prototype.addPanel = function ( winurl, winname )
71
{
72
  var aTitle = winname;
73
//  var aContentURL = "chrome://zoolmark/content/zoolmarkBrowser.xul";
74
  var aContentURL = winurl;
75
  var aCustomizeURL = "";
76
 
77
  debug("addPanel(" + aTitle + ", " + aContentURL + ", " + aCustomizeURL + ")");
78
 
79
  // Create a "container" wrapper around the current panels to
80
  // manipulate the RDF:Seq more easily.
81
  var panel_list = this.datasource.GetTarget(this.rdf.GetResource(this.resource), this.rdf.GetResource(mySidebar.prototype.nc+"panel-list"), true);
82
  if (panel_list)
83
  {
84
    panel_list.QueryInterface(Components.interfaces.nsIRDFResource);
85
  }
86
  else
87
  {
88
    // Datasource is busted. Start over.
89
    debug("Sidebar datasource is busted\n");
90
  }
91
 
92
  var container = Components.classes[CONTAINER_CONTRACTID].createInstance(nsIRDFContainer);
93
  container.Init(this.datasource, panel_list);
94
 
95
  /* Create a resource for the new panel and add it to the list */
96
  var panel_resource = this.rdf.GetResource("urn:sidebar:3rdparty-panel:" + aContentURL);
97
  var panel_index = container.IndexOf(panel_resource);
98
  if (panel_index != -1) // panel is already in list
99
  {
100
    /* do something */
101
    debug("addPanel(): panel already in list");
102
    alert( aTitle + " already exists in your sidebar." );
103
    return;
104
  }
105
 
106
  /* Now make some sidebar-ish assertions about it... */
107
  this.datasource.Assert(panel_resource, this.rdf.GetResource(this.nc + "title"), this.rdf.GetLiteral(aTitle), true);
108
  this.datasource.Assert(panel_resource, this.rdf.GetResource(this.nc + "content"), this.rdf.GetLiteral(aContentURL), true);
109
  if (aCustomizeURL)
110
    this.datasource.Assert(panel_resource, this.rdf.GetResource(this.nc + "customize"), this.rdf.GetLiteral(aCustomizeURL), true);
111
  container.AppendElement(panel_resource);
112
 
113
  // Use an assertion to pass a "refresh" event to all the sidebars.
114
  // They use observers to watch for this assertion (in sidebarOverlay.js).
115
  this.datasource.Assert(this.rdf.GetResource(this.resource), this.rdf.GetResource(this.nc + "refresh"), this.rdf.GetLiteral("true"), true);
116
  this.datasource.Unassert(this.rdf.GetResource(this.resource), this.rdf.GetResource(this.nc + "refresh"), this.rdf.GetLiteral("true"));
117
 
118
  /* Write the modified panels out. */
119
  this.datasource.QueryInterface(nsIRDFRemoteDataSource).Flush();
120
 
121
  alert( aTitle + " successfully added to your sidebar." );
122
}
123
 
124
/* static functions */
125
if (DEBUG)
126
  debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
127
else
128
  debug = function (s) {}
129
 
130
function getSidebarDatasourceURI(panels_file_id)
131
{
132
  try
133
  {
134
    /* use the fileLocator to look in the profile directory
135
     * to find 'panels.rdf', which is the
136
     * database of the user's currently selected panels. */
137
    var directory_service = Components.classes[DIR_SERV_CONTRACTID].getService();
138
    if (directory_service)
139
      directory_service = directory_service.QueryInterface(Components.interfaces.nsIProperties);
140
 
141
    /* if <profile>/panels.rdf doesn't exist, get will copy
142
     * bin/defaults/profile/panels.rdf to <profile>/panels.rdf */
143
    var sidebar_file = directory_service.get(panels_file_id, Components.interfaces.nsIFile);
144
    debug( "sidebar_file = " + sidebar_file.path );
145
 
146
    if (!sidebar_file.exists())
147
    {
148
      /* this should not happen, as GetFileLocation() should copy
149
       * defaults/panels.rdf to the users profile directory */
150
      debug("sidebar file does not exist");
151
      return null;
152
    }
153
 
154
    var io_service = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
155
    var url = io_service.newFileURI(sidebar_file).QueryInterface(Components.interfaces.nsIFileURL);
156
 
157
    debug( "URL == " + url.spec);
158
 
159
    return url.spec;
160
  }
161
  catch (ex)
162
  {
163
    /* this should not happen */
164
    debug("caught " + ex + " getting sidebar datasource uri");
165
    return null;
166
  }
167
}