Mercurial > pidgin
annotate doc/PERL-HOWTO.dox @ 11316:12f6164ee2c6
[gaim-migrate @ 13518]
I followed rlaager's advice and noticed these warnings when compiling.
committer: Tailor Script <tailor@pidgin.im>
author | Daniel Atallah <daniel.atallah@gmail.com> |
---|---|
date | Fri, 19 Aug 2005 16:30:52 +0000 |
parents | 93258e8fb6d2 |
children | 5c3a0e641520 |
rev | line source |
---|---|
6598 | 1 /** @page perl-howto Perl Scripting HOWTO |
2 | |
11278 | 3 @section Introduction |
4 Gaim Perl Plugins are setup very similarly to their C counterparts. Most of the API calls are implemented and are divided into pacakges. There are some significant differences between the Perl and C API. Much like the C API, the best place to seek guidances is the source located in the plugins/perl/common directory. The tutorial that follows will be example based and attempt to touch on the salient features of the embedded perl interpreter. It is also important to note that some of the C API is missing in Gaim's perl API. | |
10408 | 5 |
11278 | 6 It is possible to get Gtk2-Perl to work with Gaim's perl API, but only by encasing it in @c eval blocks for the time being until a better workaround comes up. If you are uninterested in using Gtk with your perl plugins than this still has bearing on you if you would like to use any perl modules that are dynamically loaded (i.e. @c use @c Math; ). Eventually this should be corrected. |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
7 |
11278 | 8 @section first-script Writing your first script |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
9 |
11278 | 10 Let us start with a simple example of a Gaim perl plugin. The following code sample is a complete plugin that can be copied and used as is. |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
11 |
11278 | 12 @code |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
13 use Gaim; |
6598 | 14 |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
15 %PLUGIN_INFO = ( |
11278 | 16 perl_api_version => 2, |
17 name => "Perl Test Plugin", | |
18 version => "0.1", | |
19 summary => "Test plugin for the Perl interpreter.", | |
20 description => "Your description here", | |
21 author => "John H. Kelm <johnhkelm\@gmail.com", | |
22 url => "http://gaim.sourceforge.net/", | |
6598 | 23 |
11278 | 24 load => "plugin_load", |
25 unload => "plugin_unload" | |
7182
65acffe70a6d
[gaim-migrate @ 7750]
Christian Hammond <chipx86@chipx86.com>
parents:
6720
diff
changeset
|
26 ); |
6598 | 27 |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
28 sub plugin_init { |
11278 | 29 return %PLUGIN_INFO; |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
30 } |
6598 | 31 |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
32 sub plugin_load { |
11278 | 33 my $plugin = shift; |
34 Gaim::debug_info("plugin_load()", "Test Plugin Loaded."); | |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
35 } |
6598 | 36 |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
37 sub plugin_unload { |
11278 | 38 my $plugin = shift; |
39 Gaim::debug_info("plugin_unload()", "Test Plugin Unloaded."); | |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
40 } |
11278 | 41 @endcode |
42 | |
43 It is necessary to load the Gaim perl package with the line @code use Gaim; @endcode which will make all the Gaim perl API available to the script. The @c \%PLUGIN_INFO has contains all the information that will be displayed in the Plugin frame of the Preferences dialog. In addition to information needed to describe the plugin to the user, information about how the plugin is to be handled is present. The keys @c load and @c unload specify and action to take when the plugin is loaded and when it is unloaded from the Preferences dialog respectively. There are other key values that may be present in the @c \%PLUGIN_INFO hash that will be covered in the following sections. | |
44 | |
45 The Perl subroutine @c plugin_init is executed when the plugin is probed by the plugin subsystem. What this means is as soon as Gaim is started, this subroutine is run once, regardless of whether the plugin is loaded or not. The other two subroutines present are those defined by the @c \%PLUGIN_INFO hash and take the plugin handle as an argument. When the plugin is loaded and subsequently unloaded it will print a message to the debug window using the @c Gaim::debug_info() Gaim perl API call. | |
46 | |
47 The last step is to save the script with a .pl file extention in your ~/.gaim/plugins directory. After restarting gaim the plugin "Perl Test Plugin" should now appear under "Tools->Preferences->Plugins". To view the messages make sure you run Gaim from the console with the '-d' flag or open the Debug Window from inside Gaim under "Help". When you enable the checkbox next the plugin you should see a message appear in the Debug Window (or console) and when you disable the checkbox you should see another message appear. You have now created the framework that will allow you to create almost any kind of Gaim plugin you can imagine. | |
48 | |
49 @section account-api Account and Account Option Functions | |
50 | |
51 The Account API is in the @c Gaim::Account:: and @c Gaim::Accounts:: packages and both are nearly identical to their C counterparts @c gaim_account_ and @c gaim_accounts_. The Account Option API is in the package @c Gaim::Account::Option and is identical to its C implementation @c gaim_account_option . | |
52 | |
53 The Account* APIs allow for scripts to create, remove, and edit accounts of the type GaimAccount. (Note: Gaim type have no real meaning in perl scripts other than the types of the arguments of the perl subroutines need to be of the expected type.) This section will not go into detail about the @c Gaim::Account::Option package for its use in building protocol plugins which are outside the scope of this document. However, most of the API calls for the @c Gaim::Account::Option package should function as expected so if there is a need to access any of the Account Options the function calls necessary are avaialbe in the Gaim perl API. | |
54 | |
55 To reduce redundant code the following code examples are going to use the template shown in the previous section. To highlight some of the more useful features of the Account API we will be replacing the @c plugin_load perl subroutine. For testing purposes we will display output on the command line by using perl @c print commands. | |
56 | |
57 @code | |
58 sub plugin_load { | |
59 $plugin = shift; | |
60 | |
61 # Testing was done using Oscar, but this should work regardless of the protocol chosen | |
62 my $protocol = "prpl-oscar"; | |
63 | |
64 ################################# | |
65 # # | |
66 # Gaim::Account # | |
67 # # | |
68 ################################# | |
69 | |
70 # Create a new Account | |
71 print "Testing: Gaim::Account::new()..."; | |
72 $account = Gaim::Account::new("TEST_NAME", $protocol); | |
73 if ($account) { print "ok.\n"; } else { print "fail.\n"; } | |
74 | |
75 # Add a new Account | |
76 print "Testing: Gaim::Account::add()..."; | |
77 Gaim::Accounts::add($account); | |
78 print "pending find...\n"; | |
6598 | 79 |
11278 | 80 # Find the account we just added to verify its existence |
81 print "Testing: Gaim::Accounts::find()..."; | |
82 $account = Gaim::Accounts::find("TEST_NAME", $protocol); | |
83 if ($account) { print "ok.\n"; } else { print "fail.\n"; } | |
6598 | 84 |
11278 | 85 # Return the username |
86 print "Testing: Gaim::Account::get_username()..."; | |
87 $user_name = Gaim::Account::get_username($account); | |
88 if ($user_name) { print $user_name . "...ok.\n"; } else { print "fail.\n"; } | |
89 | |
90 # Verify if the user is connected | |
91 print "Testing: Gaim::Account::is_connected()"; | |
92 $user_connected = Gaim::Account::is_connected($account); | |
93 if (!($user_connected)) { print "...not connected...ok..\n"; } else { print "...connected...ok.\n"; } | |
94 | |
95 # The status mechanism is how users are Connected, set Away, Disconnected (status set to Offline), etc | |
96 # $status is now a Gaim::Status perl type. | |
97 print "Testing: Gaim::Accounts::get_active_status()..."; | |
98 $status = Gaim::Account::get_active_status($account); | |
99 if ($status) { print "ok.\n"; } else { print "fail.\n"; } | |
100 | |
101 # It follows that to connect a user you mest set the account status to "available" | |
102 # similarly we can disconnect a user by setting the account status to "offline" | |
103 $account = Gaim::Accounts::find("TEST_NAME", $protocol); | |
104 print "Testing: Gaim::Accounts::connect()...pending...\n"; | |
105 | |
106 Gaim::Account::set_status($account, "available", TRUE); | |
107 Gaim::Account::connect($account); | |
108 } | |
109 @endcode | |
110 | |
111 For the most part the code listed above is explained by the comments, however there are a few other points to make. The variables above are all specialized Perl types that contain pointers to the actual Gaim types. They can be reasigned at will just like any other variable in Perl. The only way to edit the values of a Gaim type from within perl are through accessor methods such as @c Gaim::Account::get_username(). For arguments that you would make @c NULL in C should be set to @c undef in Perl. | |
112 | |
113 @section buddylist-api Buddylist, Group and Chat API | |
114 | |
115 The BuddList, Group and Chat APIs are very similar and whatever is shown for the @c Gaim::BuddlyList API should carry over to @c Gaim::Chat and @c Gaim::Group. Note that there is a @c Gaim::Find pacakge that was created to keep the naming consistent with how these functions are named in the C API. | |
116 | |
117 @code | |
118 sub plugin_load { | |
119 my $plugin = shift; | |
120 my $protocol = "prpl-oscar"; | |
121 | |
122 # This is how we get an account to use in the following tests. You should replace the username | |
123 # with an existent user | |
124 $account = Gaim::Accounts::find("USERNAME", $protocol); | |
6598 | 125 |
11278 | 126 ################################# |
127 # # | |
128 # Gaim::BuddyList # | |
129 # # | |
130 ################################# | |
131 | |
132 # Testing a find function: Note Gaim::Find not Gaim::Buddy:find! | |
133 # Furthermore, this should work the same for chats and groups | |
134 print "Testing: Gaim::Find::buddy()..."; | |
135 $buddy = Gaim::Find::buddy($account, "BUDDYNAME"); | |
136 if ($buddy) { print "ok.\n"; } else { print "fail.\n"; } | |
137 | |
138 # If you should need the handle for some reason, here is how you do it | |
139 print "Testing: Gaim::BuddyList::get_handle()..."; | |
140 $handle = Gaim::BuddyList::get_handle(); | |
141 if ($handle) { print "ok.\n"; } else { print "fail.\n"; } | |
142 | |
143 # This gets the Gaim::BuddyList and references it by $blist | |
144 print "Testing: Gaim::BuddyList::get_blist()..."; | |
145 $blist = Gaim::BuddyList::get_blist(); | |
146 if ($blist) { print "ok.\n"; } else { print "fail.\n"; } | |
147 | |
148 # This is how you would add a buddy named "NEWNAME" with the alias "ALIAS" | |
149 print "Testing: Gaim::Buddy::new..."; | |
150 $buddy = Gaim::Buddy::new($account, "NEWNAME", "ALIAS"); | |
151 if ($buddy) { print "ok.\n"; } else { print "fail.\n"; } | |
152 | |
153 # Here we add the new buddy '$buddy' to the group "GROUP" | |
154 # so first we must find the group | |
155 print "Testing: Gaim::Find::group..."; | |
156 $group = Gaim::Find::group("GROUP"); | |
157 if ($group) { print "ok.\n"; } else { print "fail.\n"; } | |
158 | |
159 # To add the buddy we need to have the buddy, contact, group and node for insertion. | |
160 # For this example we can let contact be undef and set the insertion node as the group | |
161 print "Testing: Gaim::BuddyList::add_buddy..."; | |
162 Gaim::BuddyList::add_buddy($buddy, undef, $group, $group); | |
163 if ($buddy) { print "ok.\n"; } else { print "fail.\n"; } | |
6598 | 164 |
11278 | 165 # The example that follows gives an indiction of how an API call that returns a list is handled. |
166 # In this case the buddies of the account found earlier are retrieved and put in an array '@buddy_array' | |
167 # Further down an accessor method is used, 'get_name()' -- see source for details on the full set of methods | |
168 print "Testing: Gaim::Find::buddies...\n"; | |
169 @buddy_array = Gaim::Find::buddies($account, "USERNAME"); | |
170 if (@buddy_array) { | |
171 print "Buddies in list (" . @buddy_array . "): \n"; | |
172 foreach $bud (@buddy_array) { | |
173 print Gaim::Buddy::get_name($bud) . "\n"; | |
174 } | |
175 } | |
176 } | |
177 @endcode | |
178 | |
179 The BuddyList API allows for plugins to edit buddies in the list, find the buddies on a given account, set alias, and manipulate the structer as needed. It is also contains the methods for accessing @c Gaim::Group and @c Gaim::Chat types. | |
180 | |
181 @section conn-api Connection API | |
182 | |
183 The @c Gaim::Connection API is one of the many packages that will not be covered in depth in this tutorial. They are more useful to protocol plugin developers. However, the entire @c gaim_connection_ API has corresponding, functioning perl subroutines. | |
184 | |
185 @section conv-api Conversation API | |
186 | |
187 The Gaim perl API for @c gaim_conversation_ and @c gaim_conv_window_ allow plugins to interact with open conversations, create new conversations, and modify conversations at will. The following example again replaces the @c plugin_load subroutine. In the example script, a new window is created, displayed and a new conversation instant message is created. The @c Gaim::Conv::Chat package handles the @c gaim_conv_chat_ portion of the API very similarly to the examples that follow. | |
188 | |
189 @code | |
190 sub plugin_load { | |
191 my $plugin = shift; | |
192 my $protocol = "prpl-oscar"; | |
193 | |
194 $account = Gaim::Accounts::find("USERNAME", $protocol); | |
195 | |
196 ################################# | |
197 # # | |
198 # Gaim::Conv # | |
199 # # | |
200 ################################# | |
201 | |
202 # First we create two new conversations. | |
203 print "Testing Gaim::Conv::new()..."; | |
204 $conv1 = Gaim::Conv::new(1, $account, "Test Conv. 1"); | |
205 if ($conv1) { print "ok.\n"; } else { print "fail.\n"; } | |
206 | |
207 print "Testing Gaim::Conv::new()..."; | |
208 $conv2 = Gaim::Conv::new(1, $account, "Test Conv. 2"); | |
209 if ($conv2) { print "ok.\n"; } else { print "fail.\n"; } | |
210 | |
211 # Second we create a window to display the conversations in. | |
212 # Note that the package here is Gaim::Conv::Window | |
213 print "Testing Gaim::Conv::Window::new()...\n"; | |
214 $win = Gaim::Conv::Window::new(); | |
215 | |
216 # The third thing to do is to add the two conversations to the windows. | |
217 # The subroutine add_conversation() returns the number of conversations present in the window. | |
218 print "Testing Gaim::Conv::Window::add_conversation()..."; | |
219 $conv_count = Gaim::Conv::Window::add_conversation($win, $conv1); | |
220 if ($conv_count) { print "ok..." . $conv_count . " conversations...\n"; } else { print "fail.\n"; } | |
6598 | 221 |
11278 | 222 print "Testing Gaim::Conv::Window::add_conversation()..."; |
223 $conv_count = Gaim::Conv::Window::add_conversation($win, $conv2); | |
224 if ($conv_count) { print "ok..." . $conv_count . " conversations...\n"; } else { print "fail.\n"; } | |
225 | |
226 # Now the window is displayed to the user. | |
227 print "Testing Gaim::Conv::Window::show()...\n"; | |
228 Gaim::Conv::Window::show($win); | |
229 | |
230 # Use Gaim::Conv::get_im_data to get a handle for the conversation | |
231 print "Testing Gaim::Conv::get_im_data()...\n"; | |
232 $im = Gaim::Conv::get_im_data($conv1); | |
233 if ($im) { print "ok.\n"; } else { print "fail.\n"; } | |
234 | |
235 # Here we send messages to the conversation | |
236 print "Testing Gaim::Conv::IM::send()...\n"; | |
237 Gaim::Conv::IM::send($im, "Message Test."); | |
238 | |
239 print "Testing Gaim::Conv::IM::write()...\n"; | |
240 Gaim::Conv::IM::write($im, "SENDER", "<b>Message</b> Test.", 0, 0); | |
241 } | |
242 @endcode | |
243 | |
244 The next block of code shows how a script can close a known conversation window @c $win. | |
245 | |
246 @code | |
247 print "Testing Gaim::Conv::Window::get_conversation_count()...\n"; | |
248 $conv_count = Gaim::Conv::Window::get_conversation_count($win); | |
249 if ($conv_count > 0) { | |
250 print "Testing Gaim::Conv::Window::destroy()...\n"; | |
251 Gaim::Conv::Window::destroy($win); | |
252 } | |
253 @endcode | |
254 | |
255 @section plugin-pref-api Plugin Preference and Gtk Preference API | |
256 | |
257 The plugin preference API allows the plugin to display options in a preference pane that the user can change to manipulate the behaviour of the particular perl plugin. The method used for creating the pane in C does not allow a direct mapping into perl. Therefore perl plugin writers must be aware that there will be significant differences in how they create plugin preference panes. | |
258 | |
259 To first create a standard plugin preference tab we need to add some key/value pairs to the @c \%PLUGIN_INFO hash. The first line contains the perl subroutine that must be provided and will return a @c Gaim::Pref::Frame. | |
6598 | 260 |
11278 | 261 @code |
262 %PLUGIN_INFO = { | |
263 ..., | |
264 prefs_info => "prefs_info_cb" | |
265 }; | |
266 @endcode | |
267 | |
268 The perl subroutine @c prefs_info_cb will be called to create the tab for the perl plugin in the Preferences dialog. An example of this function will explain the details of creating a preference frame. However, it is necessary to first create the preferences from @c plugin_load as follows. | |
269 | |
270 @code | |
271 sub plugin_load { | |
272 my $plugin = shift; | |
273 | |
274 # Here we are adding a set of preferences | |
275 # The second argument is the default value for the preference. | |
276 Gaim::Prefs::add_none("/plugins/core/perl_test"); | |
277 Gaim::Prefs::add_bool("/plugins/core/perl_test/bool", 1); | |
278 Gaim::Prefs::add_string("/plugins/core/perl_test/choice", "ch1"); | |
279 Gaim::Prefs::add_string("/plugins/core/perl_test/text", "Foobar"); | |
280 } | |
281 @endcode | |
282 | |
283 Now we can add these preferences from inside our function specified in @c \%PLUGIN_INFO . | |
6598 | 284 |
11278 | 285 @code |
286 sub prefs_info_cb { | |
287 # The first step is to initialize the Gaim::Pref::Frame that will be returned | |
288 $frame = Gaim::Pref::frame_new(); | |
289 | |
290 # Create a new boolean option with a label "Boolean Label" and then add it to the frame | |
291 $ppref = Gaim::Pref::new_with_label("Boolean Label"); | |
292 Gaim::Pref::frame_add($frame, $ppref); | |
293 | |
294 $ppref = Gaim::Pref::new_with_name_and_label("/plugins/core/perl_test/bool", "Boolean Preference"); | |
295 Gaim::Pref::frame_add($frame, $ppref); | |
296 | |
297 # Create a set of choices. To do so we must set the type to 1 which is the numerical equivelant of | |
298 # the GaimPrefType for choice. | |
299 $ppref = Gaim::Pref::new_with_name_and_label("/plugins/core/perl_test/choice", "Choice Preference"); | |
300 Gaim::Pref::set_type($ppref, 1); | |
301 Gaim::Pref::add_choice($ppref, "ch0", $frame); | |
302 # The following will be the default value as set from plugin_load | |
303 Gaim::Pref::add_choice($ppref, "ch1", $frame); | |
304 Gaim::Pref::frame_add($frame, $ppref); | |
305 | |
306 # Create a text box. The default value will be "Foobar" as set by plugin_load | |
307 $ppref = Gaim::Pref::new_with_name_and_label("/plugins/core/perl_test/text", "Text Box Preference"); | |
308 Gaim::Pref::set_max_length($ppref, 16); | |
309 Gaim::Pref::frame_add($frame, $ppref); | |
6598 | 310 |
11278 | 311 return $frame; |
312 } | |
313 @endcode | |
314 | |
315 Using the Gtk2-Perl module for Perl it is possible to create tailored @c GtkFrame elements and display them in a preference window. Currently it is required that all dynamically loaded modules be encased in @c eval blocks otherwise the Gaim perl plugin will crash. A work around is in progress, but for the time being a perl Gtk::Frame can be created and used as the plugin preference tab. The first step is to create the proper key/value pairs in the @c \%PLUGIN_INFO hash noting that the @c prefs_info key is no longer valid. Instead the keys @c GTK_UI and @c gtk_prefs_info must be set as follows. | |
316 | |
317 @code | |
318 %PLUGIN_INFO = { | |
319 ..., | |
320 # Used to differentiate between a regular and a Gtk preference frame | |
321 GTK_UI => TRUE, | |
322 gtk_prefs_info => "gtk_prefs_info_cb", | |
323 } | |
324 @endcode | |
325 | |
326 To finish this example @c gtk_prefs_info_cb needs to be defined. To introduce some of the flexibility of using Gtk2-Perl the example also includes a button and a callback for the button. Explaining Gtk2-Perl is beyond the scope of this tutorial and more info can be found at the project's website <a href="http://gtk2-perl.sourceforge.net/">http://gtk2-perl.sourceforge.net/</a>. | |
6598 | 327 |
11278 | 328 @code |
329 # A simple call back that prints out whatever value it is given as an argument. | |
330 sub button_cb { | |
331 my $widget = shift; | |
332 my $data = shift; | |
333 print "Clicked button with message: " . $data . "\n"; | |
334 } | |
335 | |
336 sub gtk_prefs_info_cb { | |
337 # For now it is necessary to encase this code in an eval block. | |
338 # All it does is create a button that prints a message to the console and places it in the frame. | |
339 eval ' | |
340 use Glib; | |
341 use Gtk2 \'-init\'; | |
342 | |
343 $frame = Gtk2::Frame->new(\'Gtk Test Frame\'); | |
344 $button = Gtk2::Button->new(\'Print Message\'); | |
345 | |
346 $frame->set_border_width(10); | |
347 $button->set_border_width(150); | |
348 $button->signal_connect("clicked" => \&button_cb, "Message Text"); | |
349 $frame->add($button); | |
350 | |
351 $button->show(); | |
352 $frame->show(); | |
353 '; | |
354 return $frame; | |
355 } | |
356 @endcode | |
6598 | 357 |
11278 | 358 @section request-api Request Dialog Box API |
359 | |
360 The @c Gaim::Request package allows for plugins to have interactive dialog boxes without the need for creating them in Gtk creating a seperation between the user interfaace and the plugin. The portion of the Request API available to perl scripts is listed below followed by an example illustrating their use. | |
361 | |
362 These arguments are the same for each of the three request types: | |
363 @args | |
364 @arg @em handle - The plugin handle. | |
365 @arg @em title - String title for the dialog. | |
366 @arg @em primary - The first sub-heading. | |
367 @arg @em secondary - The second sub-heading. | |
368 @arg @em ok_text - The Text for the OK button. | |
369 @arg @em ok_cb - The string name of the perl subroutine to call when the OK button is clicked. | |
370 @arg @em cancel_text - The text for the Cancel button. | |
371 @arg @em cancel_cb - The string name of the perl subroutine to call when the Cancel button is clicked. | |
372 @arg @em default_value - Default text string to display in the input box. | |
373 @arg @em multiline - Boolean where true indicates multiple line input boxes are allowed. | |
374 @arg @em masked - Boolean indicating if the user can edit the text. | |
375 @arg @em hint - See source for more information - can be left blank. | |
376 @arg @em filename - String defualt file name value. | |
377 @arg @em savedialog - Boolean where true indicates use as a save file dialog and false indicates an open file dialog. | |
378 @args | |
6598 | 379 |
11278 | 380 @code |
381 # Create a simple text input box | |
382 Gaim::Request::input(handle, title, primary, secondary, default_value, multiline, masked, hint, ok_text, ok_cb, cancel_text, cancel_cb); | |
383 | |
384 # Propt user to select a file | |
385 Gaim::Request::file(handle, title, filename, savedialog, ok_cb, cancel_cb); | |
386 | |
387 # Create a unique input dialog as shown in the following example | |
388 Gaim::Request::fields(handle, title, primary, secondary, fields, ok_text, ok_cb, cancel_text, cancel_cb); | |
389 @endcode | |
390 | |
391 What follows is an example of a @c Gaim::Request::fields() dialog box with two callbacks for an OK button and a Cancel Button. | |
392 | |
393 @code | |
394 sub ok_cb_test{ | |
395 # The $fields is passed to the callback function when the button is clicked. | |
396 # To get the values they must be extracted from $fields by name. | |
397 $fields = shift; | |
398 $account = Gaim::Request::fields_get_account($fields, "acct_test"); | |
399 $int = Gaim::Request::fields_get_integer($fields, "int_test"); | |
400 $choice = Gaim::Request::fields_get_choice($fields, "ch_test"); | |
401 } | |
402 | |
403 sub cancel_cb_test{ | |
404 # Cancel does nothing but is symmetric to the ok_cb_test | |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
405 } |
6598 | 406 |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
407 sub plugin_load { |
11278 | 408 my $plugin = shift; |
6598 | 409 |
11278 | 410 # Create a group to pool together mutltiple fields. |
411 $group = Gaim::Request::field_group_new("Group Name"); | |
412 | |
413 # Each fields is created with Gaim::Request::*_new(), is made viewable with Gaim::Request::field_*_set_show_all() | |
414 # and is then added to the group with Gaim::Request::field_group_add_field() | |
6598 | 415 |
11278 | 416 # Add an account drop down list showing all active accounts |
417 $field = Gaim::Request::field_account_new("acct_test", "Account Text", undef); | |
418 Gaim::Request::field_account_set_show_all($field, 0); | |
419 Gaim::Request::field_group_add_field($group, $field); | |
6598 | 420 |
11278 | 421 # Add an integer input box |
422 $field = Gaim::Request::field_int_new("int_test", "Integer Text", 33); | |
423 Gaim::Request::field_group_add_field($group, $field); | |
6598 | 424 |
11278 | 425 # Add a list of choices |
426 $field = Gaim::Request::field_choice_new("ch_test", "Choice Text", 1); | |
427 Gaim::Request::field_choice_add($field, "Choice 0"); | |
428 Gaim::Request::field_choice_add($field, "Choice 1"); | |
429 Gaim::Request::field_choice_add($field, "Choice 2"); | |
6598 | 430 |
11278 | 431 Gaim::Request::field_group_add_field($group, $field); |
432 | |
433 # Create a Gaim::Request and add the group that was just created. | |
434 $request = Gaim::Request::fields_new(); | |
435 Gaim::Request::fields_add_group($request, $group); | |
6598 | 436 |
11278 | 437 # Display the dialog box with the input fields added earlier with the appropriate titles. |
438 Gaim::Request::fields( | |
439 $plugin, | |
440 "Request Title!", | |
441 "Primary Title", | |
442 "Secondary Title", | |
443 $request, | |
444 "Ok Text", "ok_cb_test", | |
445 "Cancel Text", "cancel_cb_test"); | |
446 } | |
447 @endcode | |
6598 | 448 |
11278 | 449 @section timeout-cb Misc: Plugin Actions, Timeouts and Callbacks |
6598 | 450 |
11278 | 451 This section of the manual covers some of the more important features that can be added to Gaim perl Plugins. Plugin actions are callback functions that are accessible to the user from the Gaim main window under "Tools->Plugin Actions". Timeouts allow a plugin to exectue a perl subroutine after a given period of time. Note that timeouts only occur once, so if the timeout must occur periodically just add a new timeout at the end of the timeout callback function. Callbacks are functions that are called when an event occurs such as a buddy signing-on or a message being received. These three tools will be discussed in the following three examples. |
452 | |
453 The Plugin Action requires the @c \%PLUGIN_INFO hash to have two key/value pairs added and a callback perl subroutine defined. Note the difference between the C API that allows for a GList of actions--only one plugin action is allowed in Gaim perl plugins. | |
6598 | 454 |
11278 | 455 @code |
456 %PLUGIN_INFO = { | |
457 ..., | |
458 # The callback subroutine that is called when "Tools->Plugin Action->Plugin Action Label" is selected | |
459 plugin_action => "plugin_action_cb", | |
460 plugin_action_label => "Plugin Action Label" | |
461 } | |
6598 | 462 |
11278 | 463 sub plugin_action_cb { |
464 # Note the function receives the plugin handle as its argument | |
465 $plugin = shift; | |
466 print "Plugin Action Selected.\n"; | |
467 } | |
468 @endcode | |
6598 | 469 |
11278 | 470 Timeouts allow a perl subroutine to be exectued after a specified time. They only occur once, so as stated earlier the timeout must be reregistered after every time it is called. |
471 | |
472 @code | |
473 sub timeout_cb { | |
474 my $plugin = shift; | |
475 print "Timeout occured."; | |
476 | |
477 # Reschedule timeout | |
478 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin); | |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
479 } |
6603
21084026030b
[gaim-migrate @ 7127]
Christian Hammond <chipx86@chipx86.com>
parents:
6602
diff
changeset
|
480 |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
481 sub plugin_load { |
11278 | 482 $plugin = shift; |
483 | |
484 # Schedule a timeout for ten seconds from now | |
485 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin); | |
486 } | |
487 @endcode | |
6598 | 488 |
11278 | 489 Callbacks are handled by creating a perl subroutine to serve as the callback and then attaching the callback to a signal. To use callbacks it is necessary to first obtain the plugin handle with the @c Gaim::Plugin::get_handle() subroutine to pass as an argument for the callback. |
490 | |
491 @code | |
492 sub signal_cb { | |
493 # The handle and the user data come in as arguments | |
494 my ($handle, $data) = @_; | |
495 print "User just connected."; | |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
496 } |
6598 | 497 |
11278 | 498 sub plugin_load { |
499 $plugin = shift; | |
500 | |
501 # User data to be given as an argument to the callback perl subroutine. | |
502 $data = ""; | |
6598 | 503 |
11278 | 504 # A pointer to the actual plugin handle needed by the callback function |
505 $plugin_handle = Gaim::Accounts::get_handle(); | |
6598 | 506 |
11278 | 507 # Connect the perl subroutine 'signal_cb' to the event 'account-connecting' |
508 Gaim::signal_connect($plugin_handle, "account-connecting", $plugin, \&signal_cb, $data); | |
509 } | |
510 @endcode | |
511 | |
512 @section Resources | |
513 @see API Documentation | |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
514 |
6598 | 515 */ |