1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/macros.h>
21 #include <sal/config.h>
25 #include "com/sun/star/uno/RuntimeException.hpp"
26 #include "com/sun/star/uno/Sequence.hxx"
27 #include "osl/file.hxx"
28 #include "osl/security.hxx"
29 #include "osl/thread.h"
30 #include "rtl/strbuf.hxx"
31 #include "rtl/ustrbuf.hxx"
33 #include "gconfaccess.hxx"
35 #define GCONF_PROXY_MODE_KEY "/system/proxy/mode"
37 #ifdef ENABLE_LOCKDOWN
38 #define GCONF_AUTO_SAVE_KEY "/apps/openoffice/auto_save"
39 #define GCONF_USER_AUTO_SAVE_KEY "/apps/openoffice/user_auto_save"
42 namespace gconfaccess
{
46 namespace uno
= css::uno
;
49 GConfClient
* getGconfClient()
51 static GConfClient
* mClient
= 0;
54 #if !GLIB_CHECK_VERSION(2,36,0)
55 /* initialize glib object type library */
59 GError
* aError
= NULL
;
60 if (!gconf_init(0, NULL
, &aError
))
62 OUString
msg("GconfBackend:GconfLayer: Cannot Initialize Gconf connection - " +
63 OUString::createFromAscii(aError
->message
));
67 throw uno::RuntimeException(msg
);
70 mClient
= gconf_client_get_default();
73 throw uno::RuntimeException("GconfBackend:GconfLayer: Cannot Initialize Gconf connection");
76 static const char * const PreloadValuesList
[] =
78 "/desktop/gnome/interface",
80 "/system/http_proxy/host",
81 "/desktop/gnome/url-handlers/mailto",
82 #ifdef ENABLE_LOCKDOWN
84 "/desktop/gnome/lockdown",
85 "/apps/openoffice/lockdown",
86 #endif // ENABLE_LOCKDOWN
90 while( PreloadValuesList
[i
] != NULL
)
91 gconf_client_preload( mClient
, PreloadValuesList
[i
++], GCONF_CLIENT_PRELOAD_ONELEVEL
, NULL
);
97 static OUString
xdg_user_dir_lookup (const char *type
)
103 osl::Security aSecurity
;
104 oslFileHandle handle
;
105 OUString aHomeDirURL
;
106 OUString aDocumentsDirURL
;
107 OUString aConfigFileURL
;
108 OUStringBuffer aUserDirBuf
;
110 if (!aSecurity
.getHomeDir( aHomeDirURL
) )
112 osl::FileBase::getFileURLFromSystemPath(OUString("/tmp"), aDocumentsDirURL
);
113 return aDocumentsDirURL
;
116 config_home
= getenv ("XDG_CONFIG_HOME");
117 if (config_home
== NULL
|| config_home
[0] == 0)
119 aConfigFileURL
= aHomeDirURL
+ "/.config/user-dirs.dirs";
123 aConfigFileURL
= OUString::createFromAscii(config_home
) + "/user-dirs.dirs";
126 if(osl_File_E_None
== osl_openFile(aConfigFileURL
.pData
, &handle
, osl_File_OpenFlag_Read
))
128 rtl::ByteSequence seq
;
129 while (osl_File_E_None
== osl_readLine(handle
, reinterpret_cast<sal_Sequence
**>(&seq
)))
131 /* Remove newline at end */
133 int len
= seq
.getLength();
134 if(len
>0 && seq
[len
-1] == '\n')
137 p
= reinterpret_cast<char *>(seq
.getArray());
138 while (*p
== ' ' || *p
== '\t')
140 if (strncmp (p
, "XDG_", 4) != 0)
143 if (strncmp (p
, type
, strlen (type
)) != 0)
146 if (strncmp (p
, "_DIR", 4) != 0)
149 while (*p
== ' ' || *p
== '\t')
154 while (*p
== ' ' || *p
== '\t')
159 if (strncmp (p
, "$HOME/", 6) == 0)
168 aUserDirBuf
= OUStringBuffer(aHomeDirURL
+ "/");
172 aUserDirBuf
= OUStringBuffer();
174 while (*p
&& *p
!= '"')
176 if ((*p
== '\\') && (*(p
+1) != 0))
178 aUserDirBuf
.append((sal_Unicode
)*p
++);
181 osl_closeFile(handle
);
185 if (aUserDirBuf
.getLength()>0 && !bError
)
187 aDocumentsDirURL
= aUserDirBuf
.makeStringAndClear();
188 osl::Directory
aDocumentsDir( aDocumentsDirURL
);
189 if( osl::FileBase::E_None
== aDocumentsDir
.open() )
190 return aDocumentsDirURL
;
192 /* Use fallbacks historical compatibility if nothing else exists */
193 return aHomeDirURL
+ "/" + OUString::createFromAscii(type
);
196 uno::Any
makeAnyOfGconfValue( GConfValue
*pGconfValue
)
198 switch( pGconfValue
->type
)
200 case GCONF_VALUE_BOOL
:
201 return uno::makeAny( (bool) gconf_value_get_bool( pGconfValue
) );
203 case GCONF_VALUE_INT
:
204 return uno::makeAny( (sal_Int32
) gconf_value_get_int( pGconfValue
) );
206 case GCONF_VALUE_STRING
:
207 return uno::makeAny( OStringToOUString( OString(
208 gconf_value_get_string(pGconfValue
) ), RTL_TEXTENCODING_UTF8
) );
211 fprintf( stderr
, "makeAnyOfGconfValue: Type not handled.\n" );
220 static void splitFontName( GConfValue
*pGconfValue
, OUString
&rName
, sal_Int16
&rHeight
)
222 OString
aFont( gconf_value_get_string( pGconfValue
) );
223 aFont
= aFont
.trim();
224 sal_Int32 nIdx
= aFont
.lastIndexOf( ' ' );
225 if (nIdx
< 1) { // urk
227 nIdx
= aFont
.getLength();
229 OString aSize
= aFont
.copy( nIdx
+ 1 );
230 rHeight
= static_cast<sal_Int16
>( aSize
.toInt32() );
233 rName
= OStringToOUString( aFont
.copy( 0, nIdx
), RTL_TEXTENCODING_UTF8
);
238 uno::Any
translateToOOo( const ConfigurationValue
&rValue
, GConfValue
*pGconfValue
)
241 switch( rValue
.nSettingId
)
243 case SETTING_PROXY_MODE
:
246 uno::Any aOriginalValue
= makeAnyOfGconfValue( pGconfValue
);
247 aOriginalValue
>>= aProxyMode
;
249 if( aProxyMode
== "manual" )
250 return uno::makeAny( (sal_Int32
) 1 );
251 else if( aProxyMode
== "none" )
252 return uno::makeAny( (sal_Int32
) 0 );
256 case SETTING_NO_PROXY_FOR
:
258 OStringBuffer aBuffer
;
259 if( (GCONF_VALUE_LIST
== pGconfValue
->type
) && (GCONF_VALUE_STRING
== gconf_value_get_list_type(pGconfValue
)) )
261 GSList
* list
= gconf_value_get_list(pGconfValue
);
262 for(; list
; list
= g_slist_next(list
))
264 aBuffer
.append(gconf_value_get_string(static_cast<GConfValue
*>(list
->data
)) + OString(";"));
266 // Remove trailing ";"
267 aBuffer
.setLength(aBuffer
.getLength()-1);
268 return uno::makeAny(OStringToOUString(aBuffer
.makeStringAndClear(), RTL_TEXTENCODING_UTF8
));
271 g_warning( "unexpected type for ignore_hosts" );
275 case SETTING_MAILER_PROGRAM
:
278 uno::Any aOriginalValue
= makeAnyOfGconfValue( pGconfValue
);
279 aOriginalValue
>>= aMailer
;
280 sal_Int32 nIndex
= 0;
281 return uno::makeAny( aMailer
.getToken( 0, ' ', nIndex
) );
284 #ifdef ENABLE_LOCKDOWN
285 // "short" values need to be returned a sal_Int16
286 case SETTING_FONT_ANTI_ALIASING_MIN_PIXEL
:
287 case SETTING_SYMBOL_SET
:
289 sal_Int32
nShortValue(0);
290 uno::Any aOriginalValue
= makeAnyOfGconfValue( pGconfValue
);
291 aOriginalValue
>>= nShortValue
;
292 return uno::makeAny( (sal_Int16
) nShortValue
);
295 #endif // ENABLE_LOCKDOWN
297 // "boolean" values that need a string to be returned
298 case SETTING_ENABLE_ACCESSIBILITY
:
299 #ifdef ENABLE_LOCKDOWN
300 case SETTING_DISABLE_PRINTING
:
301 #endif // ENABLE_LOCKDOWN
303 bool bBooleanValue
= false;
304 uno::Any aOriginalValue
= makeAnyOfGconfValue( pGconfValue
);
305 aOriginalValue
>>= bBooleanValue
;
306 return uno::makeAny( OUString::boolean( bBooleanValue
) );
309 case SETTING_WORK_DIRECTORY
:
311 OUString aDocumentsDirURL
= xdg_user_dir_lookup("Documents");
313 return uno::makeAny( aDocumentsDirURL
);
316 case SETTING_TEMPLATE_DIRECTORY
:
318 OUString aTemplatesDirURL
= xdg_user_dir_lookup("Templates");
320 return uno::makeAny( aTemplatesDirURL
);
323 case SETTING_USER_GIVENNAME
:
325 OUString
aCompleteName( OStringToOUString(
326 g_get_real_name(), osl_getThreadTextEncoding() ) );
327 sal_Int32 nIndex
= 0;
330 aGivenName
= aCompleteName
.getToken( 0, ' ', nIndex
);
331 while ( nIndex
== 0 );
333 return uno::makeAny( aGivenName
);
337 case SETTING_USER_SURNAME
:
339 OUString
aCompleteName( OStringToOUString(
340 g_get_real_name(), osl_getThreadTextEncoding() ) );
341 sal_Int32 nIndex
= 0;
344 aSurname
= aCompleteName
.getToken( 0, ' ', nIndex
);
345 while ( nIndex
>= 0 );
347 return uno::makeAny( aSurname
);
350 case SETTING_SOURCEVIEWFONT_NAME
:
351 case SETTING_SOURCEVIEWFONT_HEIGHT
:
356 splitFontName (pGconfValue
, aName
, nHeight
);
357 if (rValue
.nSettingId
== SETTING_SOURCEVIEWFONT_NAME
)
358 return uno::makeAny( aName
);
360 return uno::makeAny( nHeight
);
365 fprintf( stderr
, "Unhandled setting to translate.\n" );
374 bool SAL_CALL
isDependencySatisfied( GConfClient
* pClient
, const ConfigurationValue
&rValue
)
376 switch( rValue
.nDependsOn
)
378 case SETTING_PROXY_MODE
:
380 GConfValue
* pGconfValue
= gconf_client_get( pClient
, GCONF_PROXY_MODE_KEY
, NULL
);
382 if ( pGconfValue
!= NULL
)
384 bool bOk
= g_ascii_strcasecmp( "manual", gconf_value_get_string( pGconfValue
) ) == 0;
385 gconf_value_free( pGconfValue
);
386 if (bOk
) return true;
391 case SETTING_WORK_DIRECTORY
:
393 OUString aDocumentsDirURL
= xdg_user_dir_lookup("Documents");
394 osl::Directory
aDocumentsDir( aDocumentsDirURL
);
396 if( osl::FileBase::E_None
== aDocumentsDir
.open() )
401 case SETTING_TEMPLATE_DIRECTORY
:
403 OUString aTemplatesDirURL
= xdg_user_dir_lookup("Templates");
404 osl::Directory
aTemplatesDir( aTemplatesDirURL
);
406 if( osl::FileBase::E_None
== aTemplatesDir
.open() )
411 case SETTING_USER_GIVENNAME
:
413 OUString
aCompleteName( OStringToOUString(
414 g_get_real_name(), osl_getThreadTextEncoding() ) );
415 if( aCompleteName
!= "Unknown" )
420 case SETTING_USER_SURNAME
:
422 OUString
aCompleteName( OStringToOUString(
423 g_get_real_name(), osl_getThreadTextEncoding() ) );
424 if( aCompleteName
!= "Unknown" )
426 if( aCompleteName
.trim().indexOf( ' ' ) != -1 )
432 #ifdef ENABLE_LOCKDOWN
433 case SETTING_AUTO_SAVE
:
435 GConfValue
* pGconfValue
= gconf_client_get( pClient
, GCONF_AUTO_SAVE_KEY
, NULL
);
437 if( ( pGconfValue
!= NULL
) )
439 bool bOk
= gconf_value_get_bool( pGconfValue
);
440 gconf_value_free( pGconfValue
);
441 if (bOk
) return true;
445 case SETTING_USER_AUTO_SAVE
:
447 GConfValue
* pGconfValue
= gconf_client_get( pClient
, GCONF_USER_AUTO_SAVE_KEY
, NULL
);
449 if( ( pGconfValue
!= NULL
) )
451 bool bOk
= gconf_value_get_bool( pGconfValue
);
452 gconf_value_free( pGconfValue
);
453 if (bOk
) return true;
457 #endif // ENABLE_LOCKDOWN
460 fprintf( stderr
, "Unhandled setting to check dependency.\n" );
469 ConfigurationValue
const ConfigurationValues
[] =
472 SETTING_ENABLE_ACCESSIBILITY
,
473 "/desktop/gnome/interface/accessibility",
474 RTL_CONSTASCII_STRINGPARAM("EnableATToolSupport"),
481 GCONF_PROXY_MODE_KEY
,
482 RTL_CONSTASCII_STRINGPARAM("ooInetProxyType"),
488 SETTING_PROXY_HTTP_HOST
,
489 "/system/http_proxy/host",
490 RTL_CONSTASCII_STRINGPARAM("ooInetHTTPProxyName"),
496 SETTING_PROXY_HTTP_PORT
,
497 "/system/http_proxy/port",
498 RTL_CONSTASCII_STRINGPARAM("ooInetHTTPProxyPort"),
504 SETTING_PROXY_HTTPS_HOST
,
505 "/system/proxy/secure_host",
506 RTL_CONSTASCII_STRINGPARAM("ooInetHTTPSProxyName"),
512 SETTING_PROXY_HTTPS_PORT
,
513 "/system/proxy/secure_port",
514 RTL_CONSTASCII_STRINGPARAM("ooInetHTTPSProxyPort"),
520 SETTING_PROXY_FTP_HOST
,
521 "/system/proxy/ftp_host",
522 RTL_CONSTASCII_STRINGPARAM("ooInetFTPProxyName"),
528 SETTING_PROXY_FTP_PORT
,
529 "/system/proxy/ftp_port",
530 RTL_CONSTASCII_STRINGPARAM("ooInetFTPProxyPort"),
536 SETTING_NO_PROXY_FOR
,
537 "/system/http_proxy/ignore_hosts",
538 RTL_CONSTASCII_STRINGPARAM("ooInetNoProxy"),
544 SETTING_MAILER_PROGRAM
,
545 "/desktop/gnome/url-handlers/mailto/command",
546 RTL_CONSTASCII_STRINGPARAM("ExternalMailer"),
551 SETTING_SOURCEVIEWFONT_NAME
,
552 "/desktop/gnome/interface/monospace_font_name",
553 RTL_CONSTASCII_STRINGPARAM("SourceViewFontName"),
558 SETTING_SOURCEVIEWFONT_HEIGHT
,
559 "/desktop/gnome/interface/monospace_font_name",
560 RTL_CONSTASCII_STRINGPARAM("SourceViewFontHeight"),
566 SETTING_WORK_DIRECTORY
,
567 "/desktop/gnome/url-handlers/mailto/command", // dummy
568 RTL_CONSTASCII_STRINGPARAM("WorkPathVariable"),
570 SETTING_WORK_DIRECTORY
, // so that the existence of the dir can be checked
574 SETTING_TEMPLATE_DIRECTORY
,
575 "/desktop/gnome/url-handlers/mailto/command", // dummy
576 RTL_CONSTASCII_STRINGPARAM("TemplatePathVariable"),
578 SETTING_TEMPLATE_DIRECTORY
, // so that the existence of the dir can be checked
582 SETTING_USER_GIVENNAME
,
583 "/desktop/gnome/url-handlers/mailto/command", // dummy
584 RTL_CONSTASCII_STRINGPARAM("givenname"),
586 SETTING_USER_GIVENNAME
590 SETTING_USER_SURNAME
,
591 "/desktop/gnome/url-handlers/mailto/command", // dummy
592 RTL_CONSTASCII_STRINGPARAM("sn"),
597 #ifdef ENABLE_LOCKDOWN
599 SETTING_WRITER_DEFAULT_DOC_FORMAT
,
600 "/apps/openoffice/writer_default_document_format",
601 RTL_CONSTASCII_STRINGPARAM("TextDocumentSetupFactoryDefaultFilter"),
607 SETTING_IMPRESS_DEFAULT_DOC_FORMAT
,
608 "/apps/openoffice/impress_default_document_format",
609 RTL_CONSTASCII_STRINGPARAM("PresentationDocumentSetupFactoryDefaultFilter"),
615 SETTING_CALC_DEFAULT_DOC_FORMAT
,
616 "/apps/openoffice/calc_default_document_format",
617 RTL_CONSTASCII_STRINGPARAM("SpreadsheetDocumentSetupFactoryDefaultFilter"),
625 RTL_CONSTASCII_STRINGPARAM("AutoSaveEnabled"),
631 SETTING_USER_AUTO_SAVE
,
632 GCONF_USER_AUTO_SAVE_KEY
,
633 RTL_CONSTASCII_STRINGPARAM("UserAutoSaveEnabled"),
639 SETTING_AUTO_SAVE_INTERVAL
,
640 "/apps/openoffice/auto_save_interval",
641 RTL_CONSTASCII_STRINGPARAM("AutoSaveTimeIntervall"),
647 SETTING_DISABLE_PRINTING
,
648 "/desktop/gnome/lockdown/disable_printing",
649 RTL_CONSTASCII_STRINGPARAM("DisablePrinting"),
655 SETTING_USE_SYSTEM_FILE_DIALOG
,
656 "/apps/openoffice/use_system_file_dialog",
657 RTL_CONSTASCII_STRINGPARAM("UseSystemFileDialog"),
663 SETTING_PRINTING_MODIFIES_DOCUMENT
,
664 "/apps/openoffice/printing_modifies_doc",
665 RTL_CONSTASCII_STRINGPARAM("PrintingModifiesDocument"),
671 SETTING_SHOW_ICONS_IN_MENUS
,
672 "/apps/openoffice/show_menu_icons",
673 RTL_CONSTASCII_STRINGPARAM("ShowIconsInMenues"),
679 SETTING_SHOW_INACTIVE_MENUITEMS
,
680 "/apps/openoffice/show_menu_inactive_items",
681 RTL_CONSTASCII_STRINGPARAM("DontHideDisabledEntry"),
687 SETTING_SHOW_FONT_PREVIEW
,
688 "/apps/openoffice/show_font_preview",
689 RTL_CONSTASCII_STRINGPARAM("ShowFontBoxWYSIWYG"),
695 SETTING_SHOW_FONT_HISTORY
,
696 "/apps/openoffice/show_font_history",
697 RTL_CONSTASCII_STRINGPARAM("FontViewHistory"),
703 SETTING_USE_SYSTEM_FONT
,
704 "/apps/openoffice/use_system_font",
705 RTL_CONSTASCII_STRINGPARAM("AccessibilityIsSystemFont"),
711 SETTING_USE_FONT_ANTI_ALIASING
,
712 "/apps/openoffice/use_font_anti_aliasing",
713 RTL_CONSTASCII_STRINGPARAM("FontAntiAliasingEnabled"),
719 SETTING_FONT_ANTI_ALIASING_MIN_PIXEL
,
720 "/apps/openoffice/font_anti_aliasing_min_pixel",
721 RTL_CONSTASCII_STRINGPARAM("FontAntiAliasingMinPixelHeight"),
727 SETTING_WARN_CREATE_PDF
,
728 "/apps/openoffice/lockdown/warn_info_create_pdf",
729 RTL_CONSTASCII_STRINGPARAM("WarnCreatePDF"),
735 SETTING_WARN_PRINT_DOC
,
736 "/apps/openoffice/lockdown/warn_info_printing",
737 RTL_CONSTASCII_STRINGPARAM("WarnPrintDoc"),
743 SETTING_WARN_SAVEORSEND_DOC
,
744 "/apps/openoffice/lockdown/warn_info_saving",
745 RTL_CONSTASCII_STRINGPARAM("WarnSaveOrSendDoc"),
751 SETTING_WARN_SIGN_DOC
,
752 "/apps/openoffice/lockdown/warn_info_signing",
753 RTL_CONSTASCII_STRINGPARAM("WarnSignDoc"),
759 SETTING_REMOVE_PERSONAL_INFO
,
760 "/apps/openoffice/lockdown/remove_personal_info_on_save",
761 RTL_CONSTASCII_STRINGPARAM("RemovePersonalInfoOnSaving"),
767 SETTING_RECOMMEND_PASSWORD
,
768 "/apps/openoffice/lockdown/recommend_password_on_save",
769 RTL_CONSTASCII_STRINGPARAM("RecommendPasswordProtection"),
776 "/apps/openoffice/undo_steps",
777 RTL_CONSTASCII_STRINGPARAM("UndoSteps"),
784 "/apps/openoffice/icon_size",
785 RTL_CONSTASCII_STRINGPARAM("SymbolSet"),
791 SETTING_MACRO_SECURITY_LEVEL
,
792 "/apps/openoffice/lockdown/macro_security_level",
793 RTL_CONSTASCII_STRINGPARAM("MacroSecurityLevel"),
799 SETTING_CREATE_BACKUP
,
800 "/apps/openoffice/create_backup",
801 RTL_CONSTASCII_STRINGPARAM("CreateBackup"),
807 SETTING_WARN_ALIEN_FORMAT
,
808 "/apps/openoffice/warn_alien_format",
809 RTL_CONSTASCII_STRINGPARAM("WarnAlienFormat"),
814 #endif // ENABLE_LOCKDOWN
817 std::size_t const nConfigurationValues
=
818 sizeof ConfigurationValues
/ sizeof ConfigurationValues
[0];
820 css::beans::Optional
< css::uno::Any
> getValue(ConfigurationValue
const & data
)
822 GConfClient
* pClient
= getGconfClient();
823 if( ( data
.nDependsOn
== SETTINGS_LAST
) || isDependencySatisfied( pClient
, data
) )
825 GConfValue
* pGconfValue
= gconf_client_get( pClient
, data
.GconfItem
, NULL
);
827 if( pGconfValue
!= NULL
)
830 if( data
.bNeedsTranslation
)
831 value
= translateToOOo( data
, pGconfValue
);
833 value
= makeAnyOfGconfValue( pGconfValue
);
835 gconf_value_free( pGconfValue
);
837 return css::beans::Optional
< css::uno::Any
>(true, value
);
840 return css::beans::Optional
< css::uno::Any
>();
845 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */