PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 1ms RepoModel.GetById 2ms app.codeStats 0ms

/components/lazreport/source/sysutilsadds.pas

http://github.com/graemeg/lazarus
Pascal | 230 lines | 146 code | 30 blank | 54 comment | 12 complexity | fed2986866433b23bc29525437b290ca MD5 | raw file
  1{
  2 ***************************************************************************
  3 *                                                                         *
  4 *   This source is free software; you can redistribute it and/or modify   *
  5 *   it under the terms of the GNU Library General Public License          *
  6 *   as published by  the Free Software Foundation; either version 2 of    *
  7 *   the License, or  (at your option) any later version.                  *
  8 *                                                                         *
  9 *   This code is distributed in the hope that it will be useful, but      *
 10 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 12 *   General Public License for more details.                              *
 13 *                                                                         *
 14 *   A copy of the GNU Library General Public License is available on the  *
 15 *   Web at <http://www.gnu.org/copyleft/lgpl.html>. You can also          *
 16 *   obtain it by writing to the Free Software Foundation,                 *
 17 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 18 *                                                                         *
 19 ***************************************************************************
 20
 21  Author: Olivier GUILBAUD
 22
 23  Abstract:
 24    This code initialze all FPC Date and Numbers formats consts
 25    with the localized format.
 26
 27    The KDE configuration it's suported (Default config and User config)
 28    I have tested this unit with Linux Mandrake 10.0.
 29    
 30    For the Euro Monay symbol, I have overriten the result because the config
 31    file it's utf8 coded.
 32    
 33  History
 34   08/10/2004 OG - Create
 35------------------------------------------------------------------------------}
 36unit SysUtilsAdds;
 37
 38{$mode objfpc}{$H+}
 39
 40interface
 41
 42{$IFDEF UNIX}
 43  {$IF (FPC_VERSION > 2) or
 44       ((FPC_VERSION = 2)
 45         and ((FPC_RELEASE > 2) or
 46              ((FPC_RELEASE = 2) and (FPC_PATCH >= 1))))}
 47    {$DEFINE USECLOCALE}
 48  {$ENDIF}
 49{$ENDIF}
 50
 51{$IFDEF USECLOCALE}
 52uses
 53  Classes, SysUtils, FileUtil,
 54  {$info if compiler can't find clocale unit, you probably are using old 2.2.1 or 2.3.1 version, please upgrade}
 55  CLocale;
 56{$ENDIF}
 57
 58implementation
 59
 60{$IFDEF USECLOCALE}
 61procedure InitInternationalFormats;
 62Var i      : Integer;
 63    St     : string;
 64    Lg     : String;
 65    LstKde : TStringList;
 66    DFMT   : string;
 67    TFMT   : string;
 68    TFMTAP : string;
 69
 70   Function ConvertLinuxDateFormatToFp(Fmt,Dflt : String):string;
 71   begin
 72     try
 73       Fmt:=StringReplace(Fmt,'%x',DFMT,[rfReplaceAll]);
 74       Fmt:=StringReplace(Fmt,'%X',TFMT,[rfReplaceAll]);
 75       Fmt:=StringReplace(Fmt,'%r',TFMTAP,[rfReplaceAll]);
 76       Fmt:=StringReplace(Fmt,'%a','ddd',[rfReplaceAll]);
 77       Fmt:=StringReplace(Fmt,'%A','dddd',[rfReplaceAll]);
 78       Fmt:=StringReplace(Fmt,'%b','mmm',[rfReplaceAll]);
 79       Fmt:=StringReplace(Fmt,'%B','mmmm',[rfReplaceAll]);
 80       Fmt:=StringReplace(Fmt,'%c','c',[rfReplaceAll]);
 81       Fmt:=StringReplace(Fmt,'%d','dd',[rfReplaceAll]);
 82       Fmt:=StringReplace(Fmt,'%D','mm/dd/yy',[rfReplaceAll]);
 83       Fmt:=StringReplace(Fmt,'%e','d',[rfReplaceAll]);
 84       Fmt:=StringReplace(Fmt,'%F','yyyy-mm-dd',[rfReplaceAll]);
 85       Fmt:=StringReplace(Fmt,'%g','yy',[rfReplaceAll]);
 86       Fmt:=StringReplace(Fmt,'%G','yyyy',[rfReplaceAll]);
 87       Fmt:=StringReplace(Fmt,'%h','mm',[rfReplaceAll]);
 88       Fmt:=StringReplace(Fmt,'%H','hh',[rfReplaceAll]);
 89       Fmt:=StringReplace(Fmt,'%h','hh',[rfReplaceAll]);
 90       Fmt:=StringReplace(Fmt,'%k','h',[rfReplaceAll]);
 91       Fmt:=StringReplace(Fmt,'%l','h',[rfReplaceAll]);
 92       Fmt:=StringReplace(Fmt,'%m','mm',[rfReplaceAll]);
 93       Fmt:=StringReplace(Fmt,'%M','nn',[rfReplaceAll]);
 94       Fmt:=StringReplace(Fmt,'%p','ampm',[rfReplaceAll]);
 95       Fmt:=StringReplace(Fmt,'%R','hh:nn',[rfReplaceAll]);
 96       Fmt:=StringReplace(Fmt,'%S','ss',[rfReplaceAll]);
 97       Fmt:=StringReplace(Fmt,'%t',#9,[rfReplaceAll]);
 98       Fmt:=StringReplace(Fmt,'%T','hh:nn:ss',[rfReplaceAll]);
 99       Fmt:=StringReplace(Fmt,'%y','yy',[rfReplaceAll]);
100       Fmt:=StringReplace(Fmt,'%Y','YYYY',[rfReplaceAll]);
101       Fmt:=StringReplace(Fmt,'%%','%',[rfReplaceAll]);
102
103       //No correspond values
104       Fmt:=StringReplace(Fmt,'%n','',[rfReplaceAll]); //New line char
105       Fmt:=StringReplace(Fmt,'%C','',[rfReplaceAll]);
106       Fmt:=StringReplace(Fmt,'%E','',[rfReplaceAll]);
107       Fmt:=StringReplace(Fmt,'%j','',[rfReplaceAll]);
108       Fmt:=StringReplace(Fmt,'%o','',[rfReplaceAll]);
109       Fmt:=StringReplace(Fmt,'%s','',[rfReplaceAll]);
110       Fmt:=StringReplace(Fmt,'%u','',[rfReplaceAll]);
111       Fmt:=StringReplace(Fmt,'%U','',[rfReplaceAll]);
112       Fmt:=StringReplace(Fmt,'%V','',[rfReplaceAll]);
113       Fmt:=StringReplace(Fmt,'%W','',[rfReplaceAll]);
114       Fmt:=StringReplace(Fmt,'%w','',[rfReplaceAll]);
115       Fmt:=StringReplace(Fmt,'%+','',[rfReplaceAll]);
116       Fmt:=StringReplace(Fmt,'%z','',[rfReplaceAll]);
117       Fmt:=StringReplace(Fmt,'%Z','',[rfReplaceAll]);
118
119     finally
120       if Trim(fmt)<>'' then
121          Result:=Fmt
122       else
123         Result:=Dflt;
124     end;
125   end;
126
127   procedure ReadKdeFileConfig(aFileName,aSection : String;  alst : TStringList);
128   Var f      : Text;
129       Stc,S  : String;
130       Flag   : Boolean;
131   begin
132     if FileExistsUTF8(aFileName) then
133     begin
134       AssignFile(f,aFileName);
135       Reset(f);
136       try
137         Flag:=False;
138         While not EOF(F) do
139         begin
140           ReadLn(f,Stc);
141
142           if ((Trim(Stc)='') or (Pos('[',Stc)=1)) and Flag then
143             Break;
144
145           if AnsiSameText(Stc,aSection) then
146             Flag:=True;
147
148           if Flag then
149           begin
150             S:=UpperCase(Trim(Copy(Stc,1,Pos('=',Stc)-1)));     //Name
151             System.Delete(Stc,1,Pos('=',Stc)); //Value
152
153             //if code is Euro symbol (UTF8)
154             if Stc=#226#130#172 then
155               Stc:='ยค';
156
157             aLst.Values[S]:=Stc; //if name exists, the value is override
158           end;
159         end;
160       finally
161         CloseFile(F);
162       end;
163     end;
164   end;
165
166   function ReadKdeConfig(Ident,defaultValue : string; DateFmt : Boolean=False):string;
167   begin
168     Result:=LstKde.Values[UpperCase(Ident)];
169     if Result='' then
170       Result:=LstKde.Values[UpperCase(Format('%s[%s]',[Ident,Lg]))];
171
172
173     if (Result<>'') and DateFmt then
174       Result:=ConvertLinuxDateFormatToFp(Result,'');
175
176     if Result='' then
177       Result:=DefaultValue;
178   end;
179
180begin
181
182  //GNOME: GNOME_DESKTOP_SESSION_ID
183  //KDE config
184  if (GetEnvironmentVariable('KDE_FULL_SESSION')<>'') and
185     (DirectoryExistsUTF8(ExpandFileNameUTF8('~/.kde/share/config'))) then
186  begin
187
188    DFMT := ShortDateFormat; //nl_langinfo(D_FMT)
189    TFMT := ShortTimeFormat; //nl_langinfo(T_FMT)
190    TFMTAP := LongTimeFormat; //nl_langinfo(T_FMT_AMPM)
191
192    Lg:=Copy(GetEnvironmentVariableUTF8('LANG'),1,2); //Langue
193    LstKde:=TStringList.Create;
194    try
195      ReadKdeFileConfig(Format('/usr/share/locale/l10n/%s/entry.desktop',[Lg]),'[KCM Locale]',LstKde);
196      ReadKdeFileConfig(ExpandFileNameUTF8('~/.kde/share/config/kdeglobals'),'[Locale]',LstKde);
197
198      St:=ReadKdeConfig('DecimalSymbol',DecimalSeparator);
199      DecimalSeparator:=St[1];
200      CurrencyString:=ReadKdeConfig('CurrencySymbol',CurrencyString);
201      ThousandSeparator:=ReadKdeConfig('ThousandsSeparator',ThousandSeparator)[1];
202      LongTimeFormat:=ReadKdeConfig('TimeFormat',LongTimeFormat,True);
203      LongDateFormat:=ReadKdeConfig('DateFormat',LongDateFormat,True);
204      ShortDateFormat:=ReadKdeConfig('DateFormatShort',ShortDateFormat,True);
205      ShortTimeFormat:=ReadKdeConfig('TimeFormatShort',ShortTimeFormat,True);
206
207      //Date separator
208      for i:=1 to Length(ShortDateFormat) do
209      begin
210        if ShortDateFormat[i] in ['-','.','/'] then
211        begin
212          DateSeparator:=ShortDateFormat[i];
213          Break;
214        end;
215      end;
216      ShortDateFormat:=StringReplace(ShortDateFormat,DateSeparator,'/',[rfReplaceAll]);
217
218    finally
219      LstKde.Free;
220    end;
221  end;
222end;
223
224
225INITIALIZATION
226  InitInternationalFormats;
227{$ENDIF}
228
229end.
230