| 
									
										
										
										
											2010-07-06 09:02:35 +00:00
										 |  |  | /* | 
					
						
							|  |  |  |     ***** BEGIN LICENSE BLOCK ***** | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2010-12-26 19:05:52 +00:00
										 |  |  |     Copyright © 2009 Center for History and New Media | 
					
						
							| 
									
										
										
										
											2010-07-06 09:02:35 +00:00
										 |  |  |                      George Mason University, Fairfax, Virginia, USA | 
					
						
							|  |  |  |                      http://zotero.org
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     This file is part of Zotero. | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     Zotero is free software: you can redistribute it and/or modify | 
					
						
							| 
									
										
										
										
											2011-05-18 18:34:22 +00:00
										 |  |  |     it under the terms of the GNU Affero General Public License as published by | 
					
						
							| 
									
										
										
										
											2010-07-06 09:02:35 +00:00
										 |  |  |     the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |     (at your option) any later version. | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     Zotero is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |     but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2011-05-18 18:34:22 +00:00
										 |  |  |     GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2010-07-06 09:02:35 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2011-05-18 18:34:22 +00:00
										 |  |  |     You should have received a copy of the GNU Affero General Public License | 
					
						
							| 
									
										
										
										
											2010-07-06 09:02:35 +00:00
										 |  |  |     along with Zotero.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	Based on nsChromeExtensionHandler example code by Ed Anuff at | 
					
						
							|  |  |  | 	http://kb.mozillazine.org/Dev_:_Extending_the_Chrome_Protocol
 | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  |     ***** END LICENSE BLOCK ***** | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-02 22:04:04 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * The class representing the Zotero command line handler | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function ZoteroCommandLineHandler() {} | 
					
						
							|  |  |  | ZoteroCommandLineHandler.prototype = { | 
					
						
							|  |  |  | 	/* nsICommandLineHandler */ | 
					
						
							| 
									
										
										
										
											2020-07-05 17:44:26 -04:00
										 |  |  | 	handle: async function (cmdLine) { | 
					
						
							| 
									
										
										
										
											2011-07-02 22:04:04 +00:00
										 |  |  | 		// handler for Zotero integration commands
 | 
					
						
							|  |  |  | 		// this is typically used on Windows only, via WM_COPYDATA rather than the command line
 | 
					
						
							|  |  |  | 		var agent = cmdLine.handleFlagWithParam("ZoteroIntegrationAgent", false); | 
					
						
							|  |  |  | 		if(agent) { | 
					
						
							|  |  |  | 			// Don't open a new window
 | 
					
						
							|  |  |  | 			cmdLine.preventDefault = true; | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			var command = cmdLine.handleFlagWithParam("ZoteroIntegrationCommand", false); | 
					
						
							|  |  |  | 			var docId = cmdLine.handleFlagWithParam("ZoteroIntegrationDocument", false); | 
					
						
							| 
									
										
										
										
											2021-02-10 11:31:06 +02:00
										 |  |  | 			var templateVersion = parseInt(cmdLine.handleFlagWithParam("ZoteroIntegrationTemplateVersion", false)); | 
					
						
							|  |  |  | 			templateVersion = isNaN(templateVersion) ? 0 : templateVersion; | 
					
						
							| 
									
										
										
										
											2011-07-02 22:04:04 +00:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2021-02-10 11:31:06 +02:00
										 |  |  | 			zContext.Zotero.Integration.execCommand(agent, command, docId, templateVersion); | 
					
						
							| 
									
										
										
										
											2011-07-02 22:04:04 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-02-14 02:18:40 +02:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2023-07-11 05:34:04 -04:00
										 |  |  | 		var fileToOpen; | 
					
						
							|  |  |  | 		// Handle zotero:// and file URIs and prevent them from opening a new window
 | 
					
						
							|  |  |  | 		var param = cmdLine.handleFlagWithParam("url", false); | 
					
						
							|  |  |  | 		if (param) { | 
					
						
							|  |  |  | 			cmdLine.preventDefault = true; | 
					
						
							| 
									
										
										
										
											2017-03-17 17:08:03 -04:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2023-07-11 05:34:04 -04:00
										 |  |  | 			var uri = cmdLine.resolveURI(param); | 
					
						
							|  |  |  | 			if (uri.schemeIs("zotero")) { | 
					
						
							| 
									
										
										
										
											2016-12-16 03:18:29 -05:00
										 |  |  | 				addInitCallback(function (Zotero) { | 
					
						
							|  |  |  | 					Zotero.uiReadyPromise | 
					
						
							|  |  |  | 					.then(function () { | 
					
						
							| 
									
										
										
										
											2023-07-11 05:34:04 -04:00
										 |  |  | 						// Check for existing window and focus it
 | 
					
						
							|  |  |  | 						var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] | 
					
						
							|  |  |  | 							.getService(Components.interfaces.nsIWindowMediator); | 
					
						
							|  |  |  | 						var win = wm.getMostRecentWindow("navigator:browser"); | 
					
						
							|  |  |  | 						if (win) { | 
					
						
							|  |  |  | 							win.focus(); | 
					
						
							|  |  |  | 							win.ZoteroPane.loadURI(uri.spec) | 
					
						
							| 
									
										
										
										
											2016-12-16 03:18:29 -05:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-07-11 05:34:04 -04:00
										 |  |  | 			// See below
 | 
					
						
							|  |  |  | 			else if (uri.schemeIs("file")) { | 
					
						
							|  |  |  | 				fileToOpen = OS.Path.fromFileURI(uri.spec) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							|  |  |  | 				dump(`Not handling URL: ${uri.spec}\n\n`); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		// In Fx49-based Mac Standalone, if Zotero is closed, an associated file is launched, and
 | 
					
						
							|  |  |  | 		// Zotero hasn't been opened before, a -file parameter is passed and two main windows open.
 | 
					
						
							|  |  |  | 		// Subsequent file openings when closed result in -url with file:// URLs (converted above)
 | 
					
						
							|  |  |  | 		// and don't result in two windows. Here we prevent the double window.
 | 
					
						
							|  |  |  | 		param = fileToOpen; | 
					
						
							|  |  |  | 		if (!param) { | 
					
						
							|  |  |  | 			param = cmdLine.handleFlagWithParam("file", false); | 
					
						
							|  |  |  | 			if (param && isMac()) { | 
					
						
							|  |  |  | 				cmdLine.preventDefault = true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (param) { | 
					
						
							|  |  |  | 			addInitCallback(function (Zotero) { | 
					
						
							|  |  |  | 				// Wait to handle things that require the UI until after it's loaded
 | 
					
						
							|  |  |  | 				Zotero.uiReadyPromise | 
					
						
							|  |  |  | 				.then(function () { | 
					
						
							|  |  |  | 					var file = Zotero.File.pathToFile(param); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					if(file.leafName.substr(-4).toLowerCase() === ".csl" | 
					
						
							|  |  |  | 							|| file.leafName.substr(-8).toLowerCase() === ".csl.txt") { | 
					
						
							|  |  |  | 						// Install CSL file
 | 
					
						
							|  |  |  | 						Zotero.Styles.install({ file: file.path }, file.path); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						// Ask before importing
 | 
					
						
							|  |  |  | 						var checkState = { | 
					
						
							|  |  |  | 							value: Zotero.Prefs.get('import.createNewCollection.fromFileOpenHandler') | 
					
						
							|  |  |  | 						}; | 
					
						
							| 
									
										
										
										
											2023-12-04 05:19:57 -05:00
										 |  |  | 						if (Services.prompt.confirmCheck(null, Zotero.getString('ingester.importFile.title'), | 
					
						
							| 
									
										
										
										
											2023-07-11 05:34:04 -04:00
										 |  |  | 								Zotero.getString('ingester.importFile.text', [file.leafName]), | 
					
						
							|  |  |  | 								Zotero.getString('ingester.importFile.intoNewCollection'), | 
					
						
							|  |  |  | 								checkState)) { | 
					
						
							|  |  |  | 							Zotero.Prefs.set( | 
					
						
							|  |  |  | 								'import.createNewCollection.fromFileOpenHandler', checkState.value | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							 | 
					
						
							|  |  |  | 							// Perform file import in front window
 | 
					
						
							|  |  |  | 							var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] | 
					
						
							|  |  |  | 							   .getService(Components.interfaces.nsIWindowMediator); | 
					
						
							|  |  |  | 							var browserWindow = wm.getMostRecentWindow("navigator:browser"); | 
					
						
							|  |  |  | 							browserWindow.Zotero_File_Interface.importFile({ | 
					
						
							|  |  |  | 								file, | 
					
						
							|  |  |  | 								createNewCollection: checkState.value | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-04-16 17:35:38 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-07-02 22:04:04 +00:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	classID: Components.ID("{531828f8-a16c-46be-b9aa-14845c3b010f}"), | 
					
						
							|  |  |  | 	service: true, | 
					
						
							|  |  |  | 	_xpcom_categories: [{category:"command-line-handler", entry:"m-zotero"}], | 
					
						
							| 
									
										
										
										
											2020-06-30 02:08:08 -04:00
										 |  |  | 	QueryInterface: ChromeUtils.generateQI([Components.interfaces.nsICommandLineHandler]) | 
					
						
							| 
									
										
										
										
											2011-07-02 22:04:04 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-03 04:57:32 -04:00
										 |  |  | var NSGetFactory = ComponentUtils.generateNSGetFactory([ZoteroService, ZoteroCommandLineHandler]); |