View source with raw comments or as raw
   1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
   2
   3    Author:        Jan Wielemaker and Anjo Anjewierden
   4    E-mail:        J.Wielemaker@cs.vu.nl
   5    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
   6    Copyright (c)  2002-2015, University of Amsterdam
   7                              VU University Amsterdam
   8    All rights reserved.
   9
  10    Redistribution and use in source and binary forms, with or without
  11    modification, are permitted provided that the following conditions
  12    are met:
  13
  14    1. Redistributions of source code must retain the above copyright
  15       notice, this list of conditions and the following disclaimer.
  16
  17    2. Redistributions in binary form must reproduce the above copyright
  18       notice, this list of conditions and the following disclaimer in
  19       the documentation and/or other materials provided with the
  20       distribution.
  21
  22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33    POSSIBILITY OF SUCH DAMAGE.
  34*/
  35
  36:- module(prolog_preferences,
  37          [ prolog_edit_preferences/1   % +What
  38          ]).
  39:- use_module(library(pce)).
  40:- use_module(library(pce_tick_box)).
  41
  42/** <module> Edit preferences files
  43
  44This  module  provides  prolog_edit_preferences/1,  which   is  used  to
  45simplify locating the preference files and provide a default if the user
  46has no such file.
  47
  48@see    library(win_menu) binds this to the Settings menu of the console on
  49        the MS-Windows version.
  50*/
  51
  52%!  prolog_edit_preferences(+What) is det.
  53%
  54%   Edit the specified user preference file.  What is one of
  55%
  56%       * =xpce=
  57%       * =prolog=
  58%
  59%   The UI components are started asynchronously in the XPCE thread.
  60
  61prolog_edit_preferences(What) :-
  62    in_pce_thread(pce_edit_preferences(What)).
  63
  64pce_edit_preferences(What) :-
  65    locate_preferences(What, File),
  66    auto_call(start_emacs),
  67    (   \+ access_file(File, exist)
  68    ->  send(@display, confirm,
  69             'Preferences file %s doesn''t exist.\nCreate it?', File),
  70        (   default_preferences(What, DefFile)
  71        ->  copy_file(DefFile, File)
  72        ;   true
  73        )
  74    ;   access_file(File, write)
  75    ->  true
  76    ;   send(@display, inform,
  77             'You cannot modify the preferences file %s', File)
  78    ),
  79    send(@emacs, goto_source_location, File).
  80
  81locate_preferences(xpce, File) :-
  82    ensure_xpce_config_dir(Dir),
  83    get(string('%s/Defaults', Dir), value, File).
  84locate_preferences(prolog, File) :-
  85    prolog_init_file(Base),
  86    (   absolute_file_name(user_profile(Base), File,
  87                           [ access(read),
  88                             file_errors(fail)
  89                           ])
  90    ->  true
  91    ;   absolute_file_name(app_preferences(Base), File,
  92                           [ access(write),
  93                             file_errors(fail)
  94                           ])
  95    ).
  96
  97%!  prolog_init_file(-Base)
  98%
  99%   Get the base-name of the Prolog user initialization file.
 100%
 101%   @tbd This should have a public interface.
 102
 103:- if(current_predicate('$cmd_option_val'/2)).
 104prolog_init_file(Base) :-
 105    '$cmd_option_val'(init_file, Base).
 106:- else.
 107prolog_init_file(Base) :-
 108    '$option'(init_file, Base).
 109:- endif.
 110
 111
 112%!  default_preferences(+Id, -File)
 113%
 114%   If there is a default file for the preferences, return a path to
 115%   it, so the user can be presented a starting point.
 116
 117default_preferences(prolog, File) :-
 118    member(Location,
 119           [ swi('customize/swipl.ini'),
 120             swi('customize/dotswiplrc')
 121           ]),
 122    absolute_file_name(Location, File,
 123                       [ access(read),
 124                         file_errors(fail)
 125                       ]),
 126    !.
 127default_preferences(xpce, File) :-
 128    absolute_file_name(pce('Defaults.user'), File,
 129                       [ access(read),
 130                         file_errors(fail)
 131                       ]),
 132    !.
 133
 134
 135%!  ensure_xpce_config_dir(-Dir:atom)
 136%
 137%   Ensure existence of the personal XPCE config directory.
 138
 139ensure_xpce_config_dir(Dir) :-
 140    get(@pce, application_data, AppDir),
 141    (   send(AppDir, exists)
 142    ->  true
 143    ;   send(AppDir, make)
 144    ),
 145    get(AppDir, path, Dir).
 146
 147
 148copy_file(From, To) :-
 149    send(file(To), copy, From).