1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 (** Misc. functions. *)
22 module O
= Gui_options
29 let mo = Int32.mul
ko ko
31 let unit_of_string s
=
32 match String.lowercase s
with
42 let (!!) = Options.(!!)
43 let (=:=) = Options.(=:=)
47 "http://www.mldonkey.org"
50 let len = String.length
n in
51 if len > !!O.max_file_name_len
then
52 Printf.sprintf
"%s...%s" (String.sub
n 0 (!!O.max_file_name_len
- 5)) (String.sub
n (len-5) 5)
55 let is_connected state
=
57 | Connected_initiating
58 | Connected_downloading _
67 let save_gui_options gui
=
70 match gui#window#children
with
73 O.last_tab
=:= gui#current_page
;
74 let (w
,h
) = Gdk.Window.get_size w#misc#window
in
78 Options.save_with_help
Gui_options.mldonkey_gui_ini
80 let set_hpaned (hpaned
: GPack.paned
) prop
=
81 let (w1
,_
) = Gdk.Window.get_size hpaned#misc#window
in
82 let ndx1 = (w1
* !!prop
) / 100 in
83 hpaned#child1#misc#set_geometry ~width
: ndx1 ()
85 let set_vpaned (hpaned
: GPack.paned
) prop
=
86 let (_
,h1
) = Gdk.Window.get_size hpaned#misc#window
in
87 let ndy1 = (h1
* !!prop
) / 100 in
88 hpaned#child1#misc#set_geometry ~height
: ndy1 ()
91 let get_hpaned gui
(hpaned
: GPack.paned
) prop
=
93 ignore
(hpaned#child1#coerce#misc#connect#size_allocate
95 let (w1
,_
) = Gdk.Window.get_size hpaned#misc#window
in
96 prop
=:= r
.Gtk.width
* 100 / (max
1 (w1
- hpaned#handle_size
));
100 let get_vpaned gui
(hpaned
: GPack.paned
) prop
=
102 ignore
(hpaned#child1#coerce#misc#connect#size_allocate
104 let (_
,h1
) = Gdk.Window.get_size hpaned#misc#window
in
105 prop
=:= r
.Gtk.height
* 100 / (max
1 (h1
- hpaned#handle_size
));
109 let create_search query_entry max_hits net search_type
=
111 GuiTypes.search_num
= !Gui_global.search_counter
;
112 GuiTypes.search_query
= query_entry
;
113 GuiTypes.search_max_hits
= max_hits
;
114 GuiTypes.search_type
= search_type
;
115 GuiTypes.search_network
= net
;
118 incr
Gui_global.search_counter
;
121 let rec rec_description_of_query q
=
125 | Q_OR l
-> List.flatten
(List.map
rec_description_of_query l
)
127 | Q_ANDNOT
(q1
, q2
) -> rec_description_of_query q1
128 | Q_MODULE
(_
,q
) -> rec_description_of_query q
130 | Q_KEYWORDS
(_
,s) -> [s]
137 | Q_MP3_ALBUM
(_
,s) -> [s]
141 | Q_MP3_BITRATE _
-> []
144 (** Summarize a request in a few words *)
145 let description_of_query q
=
146 match rec_description_of_query q
with
149 | [s1
; s2
] -> s1^
" "^s2
150 | s1
:: s2
:: s3
:: _
-> s1^
" "^s2^
" "^s3
153 (** To pretty-print a file size (int32) *)
154 let size_of_int32 size
=
155 if !!Gui_options.use_size_suffixes
then
156 let f = Int32.to_float size
in
158 Printf.sprintf
"%.2fG" (f /. go)
161 Printf.sprintf
"%.1fM" (f /. mo)
164 Printf.sprintf
"%.1fk" (f /. ko)
170 (** To pretty-print a file size (int64) *)
171 let size_of_int64 size
=
172 if !!Gui_options.use_size_suffixes
then
173 let f = Int64.to_float size
in
175 Printf.sprintf
"%.2fG" (f /. go)
178 Printf.sprintf
"%.1fM" (f /. mo)
181 Printf.sprintf
"%.1fk" (f /. ko)
187 (** Return a color for a given name. *)
188 let color_of_name name
=
189 let accs = [| ref 0 ; ref 0 ; ref 0 |] in
190 for i
= 0 to (String.length name
) - 1 do
192 accs.(m) := !(accs.(m)) + Char.code name
.[i
]
194 let r = !(accs.(0)) mod 210 in
195 let g = !(accs.(1)) mod 210 in
196 let b = !(accs.(2)) mod 210 in
197 let s = Printf.sprintf
"#%02X%02X%02X" r g b in
200 let insert_buttons (wtool1
: GButton.toolbar
) (wtool2
: GButton.toolbar
)
201 ~text ~tooltip ~icon ~callback
() =
203 (wtool1#insert_button
206 ~icon
: (Gui_options.pixmap icon
)#coerce
210 (wtool2#insert_button
213 ~icon
: (Gui_options.pixmap
(icon ^
"_mini"))#coerce