aboutsummaryrefslogtreecommitdiffstats
path: root/src/chrome/content/tabmanager.js
blob: 297cf9fd77da54c10ce9d112cb523eb5381a34cc (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
/* 
* Tab Manager Object
*/
function mproxy_TabManager(){
	this.manage	= false;
	this.tabs 	= new Array();
	
	// Read Preferences (toggle manager on/off)
	try{
		var	oPrefs	= Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
			manage	= (oPrefs.prefHasUserValue("mproxy.tabs.manager") && oPrefs.getBoolPref("mproxy.tabs.manager") == true);
		
		// Add Pref Observer
		var oTabManagerPrefObserver = {
			observe : function(subject, topic, data){ if(data == true && manage == false) { manage = true; /*mproxy_setProxy(false)*/; } }
		};
		oPrefs	= Components.classes["@mozilla.org/preferences-service;1"].createInstance(Components.interfaces.nsIPrefBranchInternal);
		oPrefs.addObserver("mproxy.tabs.manager", oTabManagerPrefObserver, false);
	} catch(err){}
	
	
	this.getTabForBrowser	= mproxy_tab_getTabForBrowser;
	this.getTabForDocument	= mproxy_tab_getTabForDocument;
	this.tabLoading 		= mproxy_tab_tabLoading;
}

// Called on new Window/Tab 
function mproxy_tab_tabLoading(){
	
	try{
		// Verify all tabs are in list
		var aBrowsers 	= gBrowser.browsers;
		for(var i = 0; i < aBrowsers.length; i++){
			if(this.getTabForBrowser(aBrowsers[i]) == null){
				var index = this.tabs.length;
				this.tabs[index] = new mproxy_Tab(aBrowsers[i], null, index);
			}
		}
	} catch(err) {alert(err)}

}

// Return mproxy_Tab object for oBrowser
function mproxy_tab_getTabForBrowser(oBrowser){
	for(var t = 0; t < this.tabs.length; t++){
		if(this.tabs[t].browser == oBrowser)
			return this.tabs[t];
	}	
	return null;
}

// Return mproxy_Tab object for oDocument
function mproxy_tab_getTabForDocument(oDocument){
	for(var t = 0; t < this.tabs.length; t++){
		if(this.tabs[t].browser.contentDocument == oDocument)
			return this.tabs[t];
	}	
	return null;
}

/*
* Tab Object
*/
function mproxy_Tab(browser, proxy, index){
	this.browser 	= browser;
	this.proxy		= proxy;
	this.title		= "";
	this.index		= index;
	
	//alert(this.browser.contentDocument)
	
	if(this.browser != null){
		try{
			this.browser.addEventListener("focus", mproxy_tab_onfocus, true);
			this.browser.addEventListener("unload", mproxy_tab_onunload, true);
			this.browser.addEventListener("load", mproxy_tab_onloaded, true);
		} catch(err) { alert(err); }
	}
	
	this.updateTitle = function(){
		
		if(this.proxy != null && this.proxy != ""){
			
			// Get label for proxy uri
			var	oRes		= mproxy_ds_getResource(aProxies[i]);
			var sProxyName	= mproxy_ds_getValueFor(oRes, mproxy_ds_getResource(gSProxyRdfNodeName));
			
			// Set title
			if(sProxyName != null){
				this.browser.contentDocument.title = "["+ sProxyName +"] " +this.title;
			}
		}		
	}
}


/*
* Utilities
*/
	// Notify current tab of proxy change
	function mproxy_tab_notify(sUri){
		var oTab = mproxy_getTabManager().getTabForBrowser(gBrowser.selectedBrowser);
		
		oTab.proxyUri = sUri;
		oTab.updateTitle();
	}

/*
* Event Handlers
*/

	// On Tab Focus
	function mproxy_tab_onfocus(event){
		try{
			var oTab = mproxy_getTabManager().getTabForBrowser(gBrowser.selectedBrowser)
			//oTab.browser.contentDocument.title = oTab.index;
			
			//mproxy_setProxy();
		}catch(err) { }
	}
	
	// On Window Focus
	function mproxy_tab_windowFocus(event){
		try{
			if(event.target.nodeName = "tabbrowser"){
				
			}
		}catch(err) { }
	}
	
	// On tab unload
	function mproxy_tab_onunload(event){
		try{
			var oTab = mproxy_getTabManager().getTabForDocument(event.target);
			//oTab.browser.contentDocument.title = "Refreshing...";
		}catch(err) { }
	}
	
	// On tab load
	function mproxy_tab_onloaded(event){
		try{
			var oTab = mproxy_getTabManager().getTabForDocument(event.target);
			//oTab.browser.contentDocument.title = "Loaded";
		}catch(err) { }
	}