@@ -624,385 +624,7 @@ QuickFolders.Model = {
624624 QuickFolders . Model . update ( ) ; // update folders!
625625 }
626626 return true ;
627- } ,
628-
629-
630- // removed from options.js
631- // the parameters will be empty when called from HTML
632- // because there is no Preferences object in the HTML namespace
633- storeConfig : async function ( preferences , prefMap ) {
634- // see options.copyFolderEntries
635- const Cc = Components . classes ,
636- Ci = Components . interfaces ,
637- service = Cc [ "@mozilla.org/preferences-service;1" ] . getService ( Ci . nsIPrefBranch ) ,
638- util = QuickFolders . Util ,
639- prefs = QuickFolders . Preferences ,
640- sFolderString = service . getStringPref ( "QuickFolders.folders" ) ;
641- let obj = JSON . parse ( sFolderString ) ,
642- storedObj = {
643- folders : obj ,
644- general : [ ] ,
645- advanced : [ ] ,
646- layout : [ ] ,
647- userStyle : [ ]
648- } ; // wrap into "folders" subobject, so we can add more settings
649- let isLicense = ( QuickFolders . Util . licenseInfo . isExpired || QuickFolders . Util . licenseInfo . isValidated ) ;
650- if ( isLicense ) {
651- storedObj . premium = [ ] ;
652- }
653-
654- util . logDebug ( "Storing configuration..." )
655-
656- // LEGACY BRANCH - if called from background this will contain the event
657- let prefInfos = preferences . getAll ( ) ;
658- for ( let info of prefInfos ) {
659- let originId = prefMap [ info . id ] ;
660- let node = { key : info . id , val : info . value , originalId : originId } ;
661- if ( originId ) {
662- switch ( originId . substr ( 0 , 5 ) ) {
663- case 'qfpg-' : // general
664- storedObj . general . push ( node ) ;
665- break ;
666- case 'qfpa-' : // advanced
667- storedObj . advanced . push ( node ) ;
668- break ;
669- case 'qfpl-' : // layout
670- storedObj . layout . push ( node ) ;
671- break ;
672- case 'qfpp-' : // premium - make sure not to import the License without confirmation!
673- if ( isLicense )
674- storedObj . premium . push ( node ) ;
675- break ;
676- default :
677- util . logDebug ( "Not storing - unknown preference category: " + node . key ) ;
678- }
679- }
680- else {
681- util . logDebug ( "Not found - map entry for " + info . id ) ;
682- }
683- }
684-
685- // now save all color pickers.
686- let elements = document . querySelectorAll ( "[type=color]" ) ; //getElementsByTagName('html:input');
687- for ( let i = 0 ; i < elements . length ; i ++ ) {
688- let element = elements [ i ] ;
689- let node = { elementInfo : element . getAttribute ( "elementInfo" ) , val : element . value } ;
690- storedObj . userStyle . push ( node ) ;
691- }
692-
693-
694- // [issue 115] store selection for background dropdown
695- const bgKey = 'currentFolderBar.background.selection' ;
696- let backgroundSelection = prefs . getStringPref ( bgKey ) ;
697- storedObj . layout . push ( {
698- key : 'extensions.quickfolders.' + bgKey ,
699- val : backgroundSelection ,
700- originalId : 'qfpa-CurrentFolder-Selection' }
701- ) ;
702-
703- let prettifiedJson = JSON . stringify ( storedObj , null , ' ' ) ;
704- this . fileConfig ( 'save' , prettifiedJson , 'QuickFolders-Config' ) ;
705- util . logDebug ( "Configuration stored." )
706- } ,
707-
708- loadConfig : async function ( preferences ) {
709- const prefs = QuickFolders . Preferences ,
710- util = QuickFolders . Util ;
711-
712- function changePref ( pref ) {
713- let p = preferences . get ( pref . key ) ;
714- if ( p ) {
715- if ( p . _value != pref . val ) {
716- // [issue 115] fix restoring of config values
717- util . logDebug ( "Changing [" + p . id + "] " + pref . originalId + " : " + pref . val ) ;
718- p . _value = pref . val ;
719- let e = foundElements [ pref . key ] ;
720- if ( e ) {
721- switch ( e . tagName ) {
722- case 'checkbox' :
723- e . checked = pref . val ;
724- if ( e . getAttribute ( 'oncommand' ) )
725- e . dispatchEvent ( new Event ( "command" ) ) ;
726- break ;
727- case 'textbox' : // legacy
728- case 'html:input' :
729- case 'html:textarea' :
730- e . value = pref . val ;
731- if ( e . id == "currentFolderBackground" ) {
732- QuickFolders . Util . notifyTools . notifyBackground ( { func : "updateNavigationBar" } ) ;
733- // this will call QuickFolders.Interface.updateNavigationBar()
734- }
735- break ;
736- case 'menulist' :
737- e . selectedIndex = pref . val ;
738- let menuitem = e . selectedItem ;
739- if ( menuitem && menuitem . getAttribute ( 'oncommand' ) )
740- menuitem . dispatchEvent ( new Event ( "command" ) ) ;
741- break ;
742- case 'radiogroup' :
743- e . value = pref . val ;
744- if ( e . getAttribute ( 'oncommand' ) )
745- e . dispatchEvent ( new Event ( "command" ) ) ;
746- default :
747- debugger ;
748- break ;
749- }
750- }
751- }
752- }
753- }
754-
755- function readData ( dataString ) {
756- const Cc = Components . classes ,
757- Ci = Components . interfaces ,
758- service = Cc [ "@mozilla.org/preferences-service;1" ] . getService ( Ci . nsIPrefBranch ) ,
759- QI = QuickFolders . Interface ;
760- try {
761- // removes prettyfication:
762- let config = dataString . replace ( / \r ? \n | \r / , '' ) ,
763- data = JSON . parse ( config ) ,
764- entries = data . folders ,
765- isLayoutModified = false ,
766- question = util . getBundleString ( "qf.prompt.restoreFolders" ) ;
767-
768- if ( prefs . getBoolPref ( 'restoreConfig.tabs' )
769- && Services . prompt . confirm ( window , "QuickFolders" , question . replace ( "{0}" , entries . length ) ) ) {
770- for ( let ent of entries ) {
771- if ( typeof ent . tabColor === 'undefined' || ent . tabColor === 'undefined' )
772- ent . tabColor = 0 ;
773- // default the name!!
774- if ( ! ent . name ) {
775- // retrieve the name from the folder uri (prettyName)
776- let f = QuickFolders . Model . getMsgFolderFromUri ( ent . uri , false ) ;
777- if ( f )
778- ent . name = f . prettyName ;
779- else
780- ent . name = util . getNameFromURI ( ent . uri ) ;
781- }
782- }
783- if ( ! entries . length )
784- entries = [ ] ;
785- // the following function calls this.updateMainWindow() which calls this.updateFolders()
786- util . getMail3PaneWindow ( ) . QuickFolders . initTabsFromEntries ( entries ) ;
787- let invalidCount = 0 ,
788- modelEntries = util . getMail3PaneWindow ( ) . QuickFolders . Model . selectedFolders ;
789- // updateFolders() will append "invalid" property into entry of main model if folder URL cannot be found
790- for ( let i = 0 ; i < modelEntries . length ; i ++ ) {
791- if ( modelEntries [ i ] . invalid )
792- invalidCount ++ ;
793- }
794-
795- question = util . getBundleString ( "qf.prompt.loadFolders.confirm" ) ;
796- if ( invalidCount ) {
797- let wrn =
798- util . getBundleString ( "qfInvalidTabCount" ) ;
799- question = wrn . replace ( "{0}" , invalidCount ) + "\n" + question ;
800- }
801- if ( Services . prompt . confirm ( window , "QuickFolders" , question ) ) {
802- // store
803- prefs . storeFolderEntries ( entries ) ;
804- // notify all windows
805- QuickFolders . Util . notifyTools . notifyBackground ( { func : "updateAllTabs" } ) ;
806- }
807- else {
808- // roll back
809- util . getMail3PaneWindow ( ) . QuickFolders . initTabsFromEntries ( prefs . loadFolderEntries ( ) ) ;
810- }
811- delete data . folders ; // remove this part to move on to the rest of settings
812- }
813- // ====================================================================
814- // [issue 107] Restoring general / layout Settings only works if option for restoring folders also active
815- if ( prefs . getBoolPref ( 'restoreConfig.general' ) && data . general ) {
816- for ( let i = 0 ; i < data . general . length ; i ++ ) {
817- changePref ( data . general [ i ] ) ;
818- }
819- isLayoutModified = true ;
820- }
821- if ( prefs . getBoolPref ( 'restoreConfig.layout' ) ) {
822- if ( data . layout ) {
823- for ( let i = 0 ; i < data . layout . length ; i ++ ) {
824- changePref ( data . layout [ i ] ) ;
825- }
826- isLayoutModified = true ;
827- }
828- if ( data . advanced ) {
829- for ( let i = 0 ; i < data . advanced . length ; i ++ ) {
830- changePref ( data . advanced [ i ] ) ;
831- }
832- }
833-
834- if ( data . premium ) {
835- for ( let i = 0 ; i < data . premium . length ; i ++ ) {
836- changePref ( data . premium [ i ] ) ;
837- }
838- }
839- // load custom colors and restore color pickers
840- // options.styleUpdate('Toolbar', 'background-color', this.value, 'qf-StandardColors')
841-
842- if ( data . userStyle ) { // legacy
843- let elements = document . getElementsByTagName ( 'html:input' ) ;
844- for ( let i = 0 ; i < elements . length ; i ++ ) {
845- let element = elements [ i ] ;
846- try {
847- if ( element . getAttribute ( 'type' ) == 'color' ) {
848- let elementInfo = element . getAttribute ( 'elementInfo' ) ;
849- // find the matching entry from json file
850- for ( let j = 0 ; j < data . userStyle . length ; j ++ ) {
851- let jnode = data . userStyle [ j ] ;
852- if ( jnode . elementInfo == elementInfo ) {
853- // only change value if nevessary
854- if ( element . value != jnode . val ) {
855- element . value = jnode . val ; // change color picker itself
856- util . logDebug ( "Changing [" + elementInfo + "] : " + jnode . val ) ;
857- let info = jnode . elementInfo . split ( '.' ) ;
858- if ( info . length == 2 ) {
859- // global Options object doesn't exist anymore!
860- /*
861- options.styleUpdate(
862- info[0], // element name e..g. ActiveTab
863- info[1], // element style (color / background-color)
864- jnode.val,
865- element.getAttribute('previewLabel')); // preview tab / label
866- */
867- }
868- }
869- break ;
870- }
871- }
872- // QuickFolders.Preferences.setUserStyle(elementName, elementStyle, styleValue)
873- }
874- }
875- catch ( ex ) {
876- util . logException ( "Loading layout setting[" + i + "] (color picker " + element . id + ") failed:" , ex ) ;
877- }
878- }
879- }
880-
881- }
882- if ( isLayoutModified ) { // instant visual feedback
883- // update the main window layout
884- QuickFolders . Util . notifyTools . notifyBackground ( { func : "updateFoldersUI" } ) ; // replaced QI.updateObserver();
885- }
886-
887- }
888- catch ( ex ) {
889- util . logException ( "Error in QuickFolders.Model.readData():\n" , ex ) ;
890- Services . prompt . alert ( null , "QuickFolders" , util . getBundleString ( "qf.alert.pasteFolders.formatErr" ) ) ;
891- }
892- }
893-
894- // find all controls with bound preferences
895- let myprefElements = document . querySelectorAll ( "[preference]" ) ,
896- foundElements = { } ;
897- for ( let myprefElement of myprefElements ) {
898- let prefName = myprefElement . getAttribute ( "preference" ) ;
899- foundElements [ prefName ] = myprefElement ;
900- }
901-
902- QuickFolders . Model . fileConfig ( 'load' , null , null , readData ) ; // load does the reading itself?
903- return true ;
904- } ,
905-
906- fileConfig : async function ( mode , jsonData , fname , readFunction ) {
907- const Cc = Components . classes ,
908- Ci = Components . interfaces ,
909- util = QuickFolders . Util ,
910- prefs = QuickFolders . Preferences ,
911- NSIFILE = Ci . nsILocalFile || Ci . nsIFile ;
912- util . popupRestrictedFeature ( mode + "_config" , "" , 2 ) ; // save_config, load_config
913-
914- let filterText ,
915- fp = Cc [ '@mozilla.org/filepicker;1' ] . createInstance ( Ci . nsIFilePicker ) ,
916- fileOpenMode = ( mode == 'load' ) ? fp . modeOpen : fp . modeSave ;
917-
918- let dPath = prefs . getStringPref ( 'files.path' ) ;
919- if ( dPath ) {
920- let defaultPath = Cc [ "@mozilla.org/file/local;1" ] . createInstance ( NSIFILE ) ;
921- defaultPath . initWithPath ( dPath ) ;
922- if ( defaultPath . exists ( ) ) { // avoid crashes if the folder has been deleted
923- fp . displayDirectory = defaultPath ; // nsILocalFile
924- util . logDebug ( "Setting default path for filepicker: " + dPath ) ;
925- }
926- else {
927- util . logDebug ( "fileFilters()\nPath does not exist: " + dPath ) ;
928- }
929- }
930- fp . init ( window , "" , fileOpenMode ) ; // second parameter: prompt
931- filterText = util . getBundleString ( "qf.fpJsonFile" ) ;
932- fp . appendFilter ( filterText , "*.json" ) ;
933- fp . defaultExtension = 'json' ;
934- if ( mode == 'save' ) {
935- let fileName = fname ;
936- fp . defaultString = fileName + '.json' ;
937- }
938-
939- let fpCallback = function fpCallback_FilePicker ( aResult ) {
940- if ( aResult == Ci . nsIFilePicker . returnOK || aResult == Ci . nsIFilePicker . returnReplace ) {
941- if ( fp . file ) {
942- let path = fp . file . path ;
943- // Store last Path
944- util . logDebug ( "File Picker Path: " + path ) ;
945- let lastSlash = path . lastIndexOf ( "/" ) ;
946- if ( lastSlash < 0 ) lastSlash = path . lastIndexOf ( "\\" ) ;
947- let lastPath = path . substr ( 0 , lastSlash ) ;
948- util . logDebug ( "Storing Path: " + lastPath ) ;
949- prefs . setStringPref ( 'files.path' , lastPath ) ;
950-
951- const { OS } = ( typeof ChromeUtils . import == "undefined" ) ?
952- Components . utils . import ( "resource://gre/modules/osfile.jsm" , { } ) :
953- ChromeUtils . import ( "resource://gre/modules/osfile.jsm" , { } ) ;
954-
955- //localFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
956- switch ( mode ) {
957- case 'load' :
958- let promiseRead = OS . File . read ( path , { encoding : "utf-8" } ) ; // returns Uint8Array
959- promiseRead . then (
960- function readSuccess ( data ) {
961- readFunction ( data ) ;
962- } ,
963- function readFailed ( reason ) {
964- util . logDebug ( 'read() - Failure: ' + reason ) ;
965- }
966- )
967- break ;
968- case 'save' :
969- // if (aResult == Ci.nsIFilePicker.returnReplace)
970- let promiseDelete = OS . File . remove ( path ) ;
971- // defined 2 functions
972- util . logDebug ( 'Setting up promise Delete' ) ;
973- promiseDelete . then (
974- function saveJSON ( ) {
975- util . logDebug ( 'saveJSON()…' ) ;
976- // force appending correct file extension!
977- if ( ! path . toLowerCase ( ) . endsWith ( '.json' ) )
978- path += '.json' ;
979- let promiseWrite = OS . File . writeAtomic ( path , jsonData , { encoding : "utf-8" } ) ;
980- promiseWrite . then (
981- function saveSuccess ( byteCount ) {
982- util . logDebug ( 'successfully saved ' + byteCount + ' bytes to file' ) ;
983- } ,
984- function saveReject ( fileError ) { // OS.File.Error
985- util . logDebug ( 'bookmarks.save error:' + fileError ) ;
986- }
987- ) ;
988- } ,
989- function failDelete ( fileError ) {
990- util . logDebug ( 'OS.File.remove failed for reason:' + fileError ) ;
991- }
992- ) ;
993- break ;
994- }
995- }
996- }
997- }
998- fp . open ( fpCallback ) ;
999-
1000- return true ;
1001-
1002- } ,
1003-
1004-
1005-
627+ }
1006628
1007629} // Model
1008630
0 commit comments