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 "osl/file.hxx"
27 #include "osl/security.hxx"
28 #include "osl/thread.h"
29 #include "rtl/strbuf.hxx"
30 #include "rtl/ustrbuf.hxx"
31 #include "sal/macros.h"
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
, NULL
);
70 mClient
= gconf_client_get_default();
73 throw uno::RuntimeException(OUString("GconfBackend:GconfLayer: Cannot Initialize Gconf connection"),NULL
);
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
, (sal_Sequence
**)&seq
))
131 /* Remove newline at end */
133 int len
= seq
.getLength();
134 if(len
>0 && seq
[len
-1] == '\n')
137 p
= (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 /* Special case desktop for historical compatibility */
193 if (strcmp (type
, "DESKTOP") == 0)
195 return aHomeDirURL
+ "/Desktop";
199 return aHomeDirURL
+ "/Documents";
203 //------------------------------------------------------------------------------
205 uno::Any
makeAnyOfGconfValue( GConfValue
*pGconfValue
)
207 switch( pGconfValue
->type
)
209 case GCONF_VALUE_BOOL
:
210 return uno::makeAny( (sal_Bool
) gconf_value_get_bool( pGconfValue
) );
212 case GCONF_VALUE_INT
:
213 return uno::makeAny( (sal_Int32
) gconf_value_get_int( pGconfValue
) );
215 case GCONF_VALUE_STRING
:
216 return uno::makeAny( OStringToOUString( OString(
217 gconf_value_get_string(pGconfValue
) ), RTL_TEXTENCODING_UTF8
) );
220 fprintf( stderr
, "makeAnyOfGconfValue: Type not handled.\n" );
227 //------------------------------------------------------------------------------
229 static void splitFontName( GConfValue
*pGconfValue
, OUString
&rName
, sal_Int16
&rHeight
)
231 OString
aFont( gconf_value_get_string( pGconfValue
) );
232 aFont
= aFont
.trim();
233 sal_Int32 nIdx
= aFont
.lastIndexOf( ' ' );
234 if (nIdx
< 1) { // urk
236 nIdx
= aFont
.getLength();
238 OString aSize
= aFont
.copy( nIdx
+ 1 );
239 rHeight
= static_cast<sal_Int16
>( aSize
.toInt32() );
242 rName
= OStringToOUString( aFont
.copy( 0, nIdx
), RTL_TEXTENCODING_UTF8
);
245 //------------------------------------------------------------------------------
247 uno::Any
translateToOOo( const ConfigurationValue
&rValue
, GConfValue
*pGconfValue
)
250 switch( rValue
.nSettingId
)
252 case SETTING_PROXY_MODE
:
255 uno::Any aOriginalValue
= makeAnyOfGconfValue( pGconfValue
);
256 aOriginalValue
>>= aProxyMode
;
258 if( aProxyMode
== "manual" )
259 return uno::makeAny( (sal_Int32
) 1 );
260 else if( aProxyMode
== "none" )
261 return uno::makeAny( (sal_Int32
) 0 );
265 case SETTING_NO_PROXY_FOR
:
267 OStringBuffer aBuffer
;
268 if( (GCONF_VALUE_LIST
== pGconfValue
->type
) && (GCONF_VALUE_STRING
== gconf_value_get_list_type(pGconfValue
)) )
270 GSList
* list
= gconf_value_get_list(pGconfValue
);
271 for(; list
; list
= g_slist_next(list
))
273 aBuffer
.append(gconf_value_get_string((GConfValue
*) list
->data
) + OString(";"));
275 // Remove trailing ";"
276 aBuffer
.setLength(aBuffer
.getLength()-1);
277 return uno::makeAny(OStringToOUString(aBuffer
.makeStringAndClear(), RTL_TEXTENCODING_UTF8
));
280 g_warning( "unexpected type for ignore_hosts" );
284 case SETTING_MAILER_PROGRAM
:
287 uno::Any aOriginalValue
= makeAnyOfGconfValue( pGconfValue
);
288 aOriginalValue
>>= aMailer
;
289 sal_Int32 nIndex
= 0;
290 return uno::makeAny( aMailer
.getToken( 0, ' ', nIndex
) );
293 #ifdef ENABLE_LOCKDOWN
294 // "short" values need to be returned a sal_Int16
295 case SETTING_FONT_ANTI_ALIASING_MIN_PIXEL
:
296 case SETTING_SYMBOL_SET
:
298 sal_Int32
nShortValue(0);
299 uno::Any aOriginalValue
= makeAnyOfGconfValue( pGconfValue
);
300 aOriginalValue
>>= nShortValue
;
301 return uno::makeAny( (sal_Int16
) nShortValue
);
304 #endif // ENABLE_LOCKDOWN
306 // "boolean" values that need a string to be returned
307 case SETTING_ENABLE_ACCESSIBILITY
:
308 #ifdef ENABLE_LOCKDOWN
309 case SETTING_DISABLE_PRINTING
:
310 #endif // ENABLE_LOCKDOWN
312 sal_Bool bBooleanValue
= false;
313 uno::Any aOriginalValue
= makeAnyOfGconfValue( pGconfValue
);
314 aOriginalValue
>>= bBooleanValue
;
315 return uno::makeAny( OUString::valueOf( (sal_Bool
) bBooleanValue
) );
318 case SETTING_WORK_DIRECTORY
:
320 OUString aDocumentsDirURL
= xdg_user_dir_lookup("DOCUMENTS");
322 return uno::makeAny( aDocumentsDirURL
);
325 case SETTING_USER_GIVENNAME
:
327 OUString
aCompleteName( OStringToOUString(
328 g_get_real_name(), osl_getThreadTextEncoding() ) );
329 sal_Int32 nIndex
= 0;
332 aGivenName
= aCompleteName
.getToken( 0, ' ', nIndex
);
333 while ( nIndex
== 0 );
335 return uno::makeAny( aGivenName
);
339 case SETTING_USER_SURNAME
:
341 OUString
aCompleteName( OStringToOUString(
342 g_get_real_name(), osl_getThreadTextEncoding() ) );
343 sal_Int32 nIndex
= 0;
346 aSurname
= aCompleteName
.getToken( 0, ' ', nIndex
);
347 while ( nIndex
>= 0 );
349 return uno::makeAny( aSurname
);
352 case SETTING_SOURCEVIEWFONT_NAME
:
353 case SETTING_SOURCEVIEWFONT_HEIGHT
:
358 splitFontName (pGconfValue
, aName
, nHeight
);
359 if (rValue
.nSettingId
== SETTING_SOURCEVIEWFONT_NAME
)
360 return uno::makeAny( aName
);
362 return uno::makeAny( nHeight
);
367 fprintf( stderr
, "Unhandled setting to translate.\n" );
374 //------------------------------------------------------------------------------
376 sal_Bool SAL_CALL
isDependencySatisfied( GConfClient
* pClient
, const ConfigurationValue
&rValue
)
378 switch( rValue
.nDependsOn
)
380 case SETTING_PROXY_MODE
:
382 GConfValue
* pGconfValue
= gconf_client_get( pClient
, GCONF_PROXY_MODE_KEY
, NULL
);
384 if ( pGconfValue
!= NULL
)
386 bool bOk
= g_ascii_strcasecmp( "manual", gconf_value_get_string( pGconfValue
) ) == 0;
387 gconf_value_free( pGconfValue
);
388 if (bOk
) return sal_True
;
393 case SETTING_WORK_DIRECTORY
:
395 OUString aDocumentsDirURL
= xdg_user_dir_lookup("DOCUMENTS");
396 osl::Directory
aDocumentsDir( aDocumentsDirURL
);
398 if( osl::FileBase::E_None
== aDocumentsDir
.open() )
403 case SETTING_USER_GIVENNAME
:
405 OUString
aCompleteName( OStringToOUString(
406 g_get_real_name(), osl_getThreadTextEncoding() ) );
407 if( aCompleteName
!= "Unknown" )
412 case SETTING_USER_SURNAME
:
414 OUString
aCompleteName( OStringToOUString(
415 g_get_real_name(), osl_getThreadTextEncoding() ) );
416 if( aCompleteName
!= "Unknown" )
418 if( aCompleteName
.trim().indexOf( ' ' ) != -1 )
424 #ifdef ENABLE_LOCKDOWN
425 case SETTING_AUTO_SAVE
:
427 GConfValue
* pGconfValue
= gconf_client_get( pClient
, GCONF_AUTO_SAVE_KEY
, NULL
);
429 if( ( pGconfValue
!= NULL
) )
431 bool bOk
= gconf_value_get_bool( pGconfValue
);
432 gconf_value_free( pGconfValue
);
433 if (bOk
) return sal_True
;
437 case SETTING_USER_AUTO_SAVE
:
439 GConfValue
* pGconfValue
= gconf_client_get( pClient
, GCONF_USER_AUTO_SAVE_KEY
, NULL
);
441 if( ( pGconfValue
!= NULL
) )
443 bool bOk
= gconf_value_get_bool( pGconfValue
);
444 gconf_value_free( pGconfValue
);
445 if (bOk
) return sal_True
;
449 #endif // ENABLE_LOCKDOWN
452 fprintf( stderr
, "Unhandled setting to check dependency.\n" );
461 ConfigurationValue
const ConfigurationValues
[] =
464 SETTING_ENABLE_ACCESSIBILITY
,
465 "/desktop/gnome/interface/accessibility",
466 RTL_CONSTASCII_STRINGPARAM("EnableATToolSupport"),
473 GCONF_PROXY_MODE_KEY
,
474 RTL_CONSTASCII_STRINGPARAM("ooInetProxyType"),
480 SETTING_PROXY_HTTP_HOST
,
481 "/system/http_proxy/host",
482 RTL_CONSTASCII_STRINGPARAM("ooInetHTTPProxyName"),
488 SETTING_PROXY_HTTP_PORT
,
489 "/system/http_proxy/port",
490 RTL_CONSTASCII_STRINGPARAM("ooInetHTTPProxyPort"),
496 SETTING_PROXY_HTTPS_HOST
,
497 "/system/proxy/secure_host",
498 RTL_CONSTASCII_STRINGPARAM("ooInetHTTPSProxyName"),
504 SETTING_PROXY_HTTPS_PORT
,
505 "/system/proxy/secure_port",
506 RTL_CONSTASCII_STRINGPARAM("ooInetHTTPSProxyPort"),
512 SETTING_PROXY_FTP_HOST
,
513 "/system/proxy/ftp_host",
514 RTL_CONSTASCII_STRINGPARAM("ooInetFTPProxyName"),
520 SETTING_PROXY_FTP_PORT
,
521 "/system/proxy/ftp_port",
522 RTL_CONSTASCII_STRINGPARAM("ooInetFTPProxyPort"),
528 SETTING_NO_PROXY_FOR
,
529 "/system/http_proxy/ignore_hosts",
530 RTL_CONSTASCII_STRINGPARAM("ooInetNoProxy"),
536 SETTING_MAILER_PROGRAM
,
537 "/desktop/gnome/url-handlers/mailto/command",
538 RTL_CONSTASCII_STRINGPARAM("ExternalMailer"),
543 SETTING_SOURCEVIEWFONT_NAME
,
544 "/desktop/gnome/interface/monospace_font_name",
545 RTL_CONSTASCII_STRINGPARAM("SourceViewFontName"),
550 SETTING_SOURCEVIEWFONT_HEIGHT
,
551 "/desktop/gnome/interface/monospace_font_name",
552 RTL_CONSTASCII_STRINGPARAM("SourceViewFontHeight"),
558 SETTING_WORK_DIRECTORY
,
559 "/desktop/gnome/url-handlers/mailto/command", // dummy
560 RTL_CONSTASCII_STRINGPARAM("WorkPathVariable"),
562 SETTING_WORK_DIRECTORY
, // so that the existence of the dir can be checked
566 SETTING_USER_GIVENNAME
,
567 "/desktop/gnome/url-handlers/mailto/command", // dummy
568 RTL_CONSTASCII_STRINGPARAM("givenname"),
570 SETTING_USER_GIVENNAME
574 SETTING_USER_SURNAME
,
575 "/desktop/gnome/url-handlers/mailto/command", // dummy
576 RTL_CONSTASCII_STRINGPARAM("sn"),
581 #ifdef ENABLE_LOCKDOWN
583 SETTING_WRITER_DEFAULT_DOC_FORMAT
,
584 "/apps/openoffice/writer_default_document_format",
585 RTL_CONSTASCII_STRINGPARAM("TextDocumentSetupFactoryDefaultFilter"),
591 SETTING_IMPRESS_DEFAULT_DOC_FORMAT
,
592 "/apps/openoffice/impress_default_document_format",
593 RTL_CONSTASCII_STRINGPARAM("PresentationDocumentSetupFactoryDefaultFilter"),
599 SETTING_CALC_DEFAULT_DOC_FORMAT
,
600 "/apps/openoffice/calc_default_document_format",
601 RTL_CONSTASCII_STRINGPARAM("SpreadsheetDocumentSetupFactoryDefaultFilter"),
609 RTL_CONSTASCII_STRINGPARAM("AutoSaveEnabled"),
615 SETTING_USER_AUTO_SAVE
,
616 GCONF_USER_AUTO_SAVE_KEY
,
617 RTL_CONSTASCII_STRINGPARAM("UserAutoSaveEnabled"),
623 SETTING_AUTO_SAVE_INTERVAL
,
624 "/apps/openoffice/auto_save_interval",
625 RTL_CONSTASCII_STRINGPARAM("AutoSaveTimeIntervall"),
631 SETTING_DISABLE_PRINTING
,
632 "/desktop/gnome/lockdown/disable_printing",
633 RTL_CONSTASCII_STRINGPARAM("DisablePrinting"),
639 SETTING_USE_SYSTEM_FILE_DIALOG
,
640 "/apps/openoffice/use_system_file_dialog",
641 RTL_CONSTASCII_STRINGPARAM("UseSystemFileDialog"),
647 SETTING_PRINTING_MODIFIES_DOCUMENT
,
648 "/apps/openoffice/printing_modifies_doc",
649 RTL_CONSTASCII_STRINGPARAM("PrintingModifiesDocument"),
655 SETTING_SHOW_ICONS_IN_MENUS
,
656 "/apps/openoffice/show_menu_icons",
657 RTL_CONSTASCII_STRINGPARAM("ShowIconsInMenues"),
663 SETTING_SHOW_INACTIVE_MENUITEMS
,
664 "/apps/openoffice/show_menu_inactive_items",
665 RTL_CONSTASCII_STRINGPARAM("DontHideDisabledEntry"),
671 SETTING_SHOW_FONT_PREVIEW
,
672 "/apps/openoffice/show_font_preview",
673 RTL_CONSTASCII_STRINGPARAM("ShowFontBoxWYSIWYG"),
679 SETTING_SHOW_FONT_HISTORY
,
680 "/apps/openoffice/show_font_history",
681 RTL_CONSTASCII_STRINGPARAM("FontViewHistory"),
687 SETTING_ENABLE_OPENGL
,
688 "/apps/openoffice/use_opengl",
689 RTL_CONSTASCII_STRINGPARAM("OpenGL"),
695 SETTING_OPTIMIZE_OPENGL
,
696 "/apps/openoffice/optimize_opengl",
697 RTL_CONSTASCII_STRINGPARAM("OpenGL_Faster"),
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 GConfValue
* pGconfValue
;
824 if( ( data
.nDependsOn
== SETTINGS_LAST
) || isDependencySatisfied( pClient
, data
) )
826 pGconfValue
= gconf_client_get( pClient
, data
.GconfItem
, NULL
);
828 if( pGconfValue
!= NULL
)
831 if( data
.bNeedsTranslation
)
832 value
= translateToOOo( data
, pGconfValue
);
834 value
= makeAnyOfGconfValue( pGconfValue
);
836 gconf_value_free( pGconfValue
);
838 return css::beans::Optional
< css::uno::Any
>(true, value
);
841 return css::beans::Optional
< css::uno::Any
>();
846 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */