Mercurial > pidgin
annotate doc/PERL-HOWTO.dox @ 14909:c3a7d40d64bb
[gaim-migrate @ 17681]
Modified sf patch #1590052, from Ari Pollak
Fixes two possible crashes that can happen after deleting an account
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Mon, 06 Nov 2006 07:25:05 +0000 |
parents | dd0c43d42394 |
children | e53cceebebb3 |
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 |
11501
9563b768e8e2
[gaim-migrate @ 13746]
Richard Laager <rlaager@wiktel.com>
parents:
11377
diff
changeset
|
6 It is possible to get Gtk2-Perl to work with Gaim's perl API, however you must not load the module with @c use but with @c require. If you are uninterested in using Gtk with your perl plugins than this still has bearing on you if you would like to use certain perl modules that are dynamically loaded. By always using @c require instead of @c use the problem is avoided. |
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 | |
13351
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
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. |
11278 | 48 |
49 @section account-api Account and Account Option Functions | |
50 | |
12364 | 51 The Account API is in the @c Gaim::Account:: and @c Gaim::Accounts:: packages |
52 and both are nearly identical to their C counterparts @c gaim_account_ and @c | |
53 gaim_accounts_. The Account Option API is in the package @c | |
54 Gaim::Account::Option and is identical to its C implementation @c | |
55 gaim_account_option. | |
11278 | 56 |
12364 | 57 The Account* APIs allow for scripts to create, remove, and edit accounts of the |
58 type GaimAccount. (Note: Gaim types have no real meaning in perl scripts other | |
59 than the types of the arguments of the perl subroutines need to be of the | |
60 expected type.) This section will not go into detail about the @c | |
61 Gaim::Account::Option package for its use in building protocol plugins which | |
62 are outside the scope of this document. However, most of the API calls for the | |
63 @c Gaim::Account::Option package should function as expected so if there is a | |
64 need to access any of the Account Options the function calls necessary are | |
65 avaialbe in the Gaim perl API. | |
11278 | 66 |
12364 | 67 To reduce redundant code the following code examples are going to use the |
68 template shown in the previous section. To highlight some of the more useful | |
69 features of the Account API we will be replacing the @c plugin_load perl | |
70 subroutine. For testing purposes we will display output on the command line by | |
71 using perl @c print commands. | |
11278 | 72 |
73 @code | |
74 sub plugin_load { | |
75 $plugin = shift; | |
76 | |
77 # Testing was done using Oscar, but this should work regardless of the protocol chosen | |
78 my $protocol = "prpl-oscar"; | |
79 | |
80 # Create a new Account | |
12364 | 81 print "Testing: Gaim::Account::new()... "; |
82 $account = Gaim::Account->new($TEST_NAME, $PROTOCOL_ID); | |
83 if ($account) { print "ok.\n"; } else { print "fail.\n"; } | |
11278 | 84 |
85 # Add a new Account | |
86 print "Testing: Gaim::Account::add()..."; | |
87 Gaim::Accounts::add($account); | |
88 print "pending find...\n"; | |
6598 | 89 |
11278 | 90 # Find the account we just added to verify its existence |
91 print "Testing: Gaim::Accounts::find()..."; | |
92 $account = Gaim::Accounts::find("TEST_NAME", $protocol); | |
93 if ($account) { print "ok.\n"; } else { print "fail.\n"; } | |
6598 | 94 |
11278 | 95 # Return the username |
12364 | 96 print "Testing: Gaim::Account::get_username()... "; |
97 $user_name = $account->get_username(); | |
98 if ($user_name) { | |
99 print "Success: $user_name.\n"; | |
100 } else { | |
101 print "Failed!\n"; | |
102 } | |
11278 | 103 # Verify if the user is connected |
12364 | 104 print "Testing: Gaim::Account::is_connected()"; |
105 if ($account->is_connected()) { | |
106 print " Connected.\n"; | |
107 } else { | |
108 print " Disconnected.\n"; | |
109 } | |
11278 | 110 |
12364 | 111 # The status mechanism is how users are Connected, set Away, |
112 # Disconnected (status set to Offline), etc | |
11278 | 113 # $status is now a Gaim::Status perl type. |
12364 | 114 print "Testing: Gaim::Accounts::get_active_status()... "; |
115 if ($account->get_active_status()) { | |
116 print "Okay.\n"; | |
117 } else { | |
118 print "Failed!\n"; | |
119 } | |
11278 | 120 |
12364 | 121 # It follows that to connect a user you mest set the account status to |
122 # "available" similarly we can disconnect a user by setting the account | |
123 # status to "offline" | |
124 | |
11278 | 125 $account = Gaim::Accounts::find("TEST_NAME", $protocol); |
126 print "Testing: Gaim::Accounts::connect()...pending...\n"; | |
127 | |
12364 | 128 $account->set_status("available", TRUE); |
129 $account->connect(); | |
130 | |
11278 | 131 } |
132 @endcode | |
133 | |
12364 | 134 For the most part the code listed above is explained by the comments, however |
135 there are a few other points to make. The variables above are all specialized | |
136 Perl types that contain pointers to the actual Gaim types. They can be | |
137 reasigned at will just like any other variable in Perl. The only way to edit | |
138 the values of a Gaim type from within perl are through accessor methods such as | |
139 @c Gaim::Account::get_username(). For arguments that you would make @c NULL in | |
140 C should be set to @c undef in Perl. | |
11278 | 141 |
142 @section buddylist-api Buddylist, Group and Chat API | |
143 | |
144 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. | |
145 | |
146 @code | |
147 sub plugin_load { | |
148 my $plugin = shift; | |
149 my $protocol = "prpl-oscar"; | |
150 | |
151 # This is how we get an account to use in the following tests. You should replace the username | |
152 # with an existent user | |
153 $account = Gaim::Accounts::find("USERNAME", $protocol); | |
6598 | 154 |
11278 | 155 # Testing a find function: Note Gaim::Find not Gaim::Buddy:find! |
156 # Furthermore, this should work the same for chats and groups | |
157 print "Testing: Gaim::Find::buddy()..."; | |
158 $buddy = Gaim::Find::buddy($account, "BUDDYNAME"); | |
159 if ($buddy) { print "ok.\n"; } else { print "fail.\n"; } | |
160 | |
161 # If you should need the handle for some reason, here is how you do it | |
162 print "Testing: Gaim::BuddyList::get_handle()..."; | |
163 $handle = Gaim::BuddyList::get_handle(); | |
164 if ($handle) { print "ok.\n"; } else { print "fail.\n"; } | |
165 | |
166 # This gets the Gaim::BuddyList and references it by $blist | |
167 print "Testing: Gaim::BuddyList::get_blist()..."; | |
168 $blist = Gaim::BuddyList::get_blist(); | |
169 if ($blist) { print "ok.\n"; } else { print "fail.\n"; } | |
170 | |
171 # This is how you would add a buddy named "NEWNAME" with the alias "ALIAS" | |
172 print "Testing: Gaim::Buddy::new..."; | |
173 $buddy = Gaim::Buddy::new($account, "NEWNAME", "ALIAS"); | |
174 if ($buddy) { print "ok.\n"; } else { print "fail.\n"; } | |
175 | |
176 # Here we add the new buddy '$buddy' to the group "GROUP" | |
177 # so first we must find the group | |
178 print "Testing: Gaim::Find::group..."; | |
179 $group = Gaim::Find::group("GROUP"); | |
180 if ($group) { print "ok.\n"; } else { print "fail.\n"; } | |
181 | |
182 # To add the buddy we need to have the buddy, contact, group and node for insertion. | |
183 # For this example we can let contact be undef and set the insertion node as the group | |
184 print "Testing: Gaim::BuddyList::add_buddy..."; | |
185 Gaim::BuddyList::add_buddy($buddy, undef, $group, $group); | |
186 if ($buddy) { print "ok.\n"; } else { print "fail.\n"; } | |
6598 | 187 |
11278 | 188 # The example that follows gives an indiction of how an API call that returns a list is handled. |
189 # In this case the buddies of the account found earlier are retrieved and put in an array '@buddy_array' | |
190 # Further down an accessor method is used, 'get_name()' -- see source for details on the full set of methods | |
191 print "Testing: Gaim::Find::buddies...\n"; | |
192 @buddy_array = Gaim::Find::buddies($account, "USERNAME"); | |
193 if (@buddy_array) { | |
194 print "Buddies in list (" . @buddy_array . "): \n"; | |
195 foreach $bud (@buddy_array) { | |
196 print Gaim::Buddy::get_name($bud) . "\n"; | |
197 } | |
198 } | |
199 } | |
200 @endcode | |
201 | |
202 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. | |
203 | |
204 @section conn-api Connection API | |
205 | |
206 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. | |
207 | |
208 @section conv-api Conversation API | |
209 | |
12340 | 210 The Gaim perl API for @c gaim_conversation_ and @c gaim_conv_window_ allow |
211 plugins to interact with open conversations, create new conversations, and | |
212 modify conversations at will. The following example again replaces the @c | |
213 plugin_load subroutine. In the example script, a new window is created, | |
214 displayed and a new conversation instant message is created. The @c | |
215 Gaim::Conversation::Chat package handles the @c gaim_conv_chat_ portion of the | |
216 API very similarly to the examples that follow. | |
11278 | 217 |
218 @code | |
219 sub plugin_load { | |
12340 | 220 my $plugin = shift; |
221 my $protocol = "prpl-oscar"; | |
11278 | 222 |
12340 | 223 $account = Gaim::Accounts::find("USERNAME", $protocol); |
11278 | 224 |
225 # First we create two new conversations. | |
12340 | 226 print "Testing Gaim::Conversation::new()..."; |
227 $conv1 = Gaim::Conversation::new(1, $account, "Test Conversation 1"); | |
228 if ($conv1) { print "ok.\n"; } else { print "fail.\n"; } | |
11278 | 229 |
12340 | 230 print "Testing Gaim::Conversation::new()..."; |
231 $conv2 = Gaim::Conversation::new(1, $account, "Test Conversation 2"); | |
232 if ($conv2) { print "ok.\n"; } else { print "fail.\n"; } | |
11278 | 233 |
234 # Second we create a window to display the conversations in. | |
12340 | 235 # Note that the package here is Gaim::Conversation::Window |
236 print "Testing Gaim::Conversation::Window::new()...\n"; | |
237 $win = Gaim::Conversation::Window::new(); | |
11278 | 238 |
239 # The third thing to do is to add the two conversations to the windows. | |
12340 | 240 # The subroutine add_conversation() returns the number of conversations |
241 # present in the window. | |
242 print "Testing Gaim::Conversation::Window::add_conversation()..."; | |
243 $conv_count = $conv1->add_conversation(); | |
244 if ($conv_count) { | |
245 print "ok..." . $conv_count . " conversations...\n"; | |
246 } else { | |
247 print "fail.\n"; | |
248 } | |
6598 | 249 |
12340 | 250 print "Testing Gaim::Conversation::Window::add_conversation()..."; |
251 $conv_count = $win->add_conversation($conv2); | |
252 if ($conv_count) { | |
253 print "ok..." . $conv_count . " conversations...\n"; | |
254 } else { | |
255 print "fail.\n"; | |
256 } | |
11278 | 257 |
258 # Now the window is displayed to the user. | |
12340 | 259 print "Testing Gaim::Conversation::Window::show()...\n"; |
260 $win->show(); | |
11278 | 261 |
12340 | 262 # Use get_im_data() to get a handle for the conversation |
263 print "Testing Gaim::Conversation::get_im_data()...\n"; | |
264 $im = $conv1->get_im_data(); | |
265 if ($im) { print "ok.\n"; } else { print "fail.\n"; } | |
11278 | 266 |
267 # Here we send messages to the conversation | |
12340 | 268 print "Testing Gaim::Conversation::IM::send()...\n"; |
269 $im->send("Message Test."); | |
11278 | 270 |
12340 | 271 print "Testing Gaim::Conversation::IM::write()...\n"; |
272 $im->write("SENDER", "<b>Message</b> Test.", 0, 0); | |
11278 | 273 } |
274 @endcode | |
275 | |
12340 | 276 The next block of code shows how a script can close a known conversation window |
277 @c $win. | |
11278 | 278 |
279 @code | |
12340 | 280 print "Testing Gaim::Conversation::Window::get_conversation_count()...\n"; |
281 $conv_count = $win->get_conversation_count(); | |
282 print "...and it returned $conv_count.\n"; | |
283 if ($conv_count > 0) { | |
284 print "Testing Gaim::Conversation::Window::destroy()...\n"; | |
285 $win->destroy(); | |
286 } | |
11278 | 287 @endcode |
288 | |
289 @section plugin-pref-api Plugin Preference and Gtk Preference API | |
290 | |
291 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. | |
292 | |
293 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 | 294 |
11278 | 295 @code |
296 %PLUGIN_INFO = { | |
297 ..., | |
298 prefs_info => "prefs_info_cb" | |
299 }; | |
300 @endcode | |
301 | |
302 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. | |
303 | |
304 @code | |
305 sub plugin_load { | |
306 my $plugin = shift; | |
307 | |
308 # Here we are adding a set of preferences | |
309 # The second argument is the default value for the preference. | |
310 Gaim::Prefs::add_none("/plugins/core/perl_test"); | |
311 Gaim::Prefs::add_bool("/plugins/core/perl_test/bool", 1); | |
312 Gaim::Prefs::add_string("/plugins/core/perl_test/choice", "ch1"); | |
313 Gaim::Prefs::add_string("/plugins/core/perl_test/text", "Foobar"); | |
314 } | |
315 @endcode | |
316 | |
317 Now we can add these preferences from inside our function specified in @c \%PLUGIN_INFO . | |
6598 | 318 |
11278 | 319 @code |
320 sub prefs_info_cb { | |
321 # The first step is to initialize the Gaim::Pref::Frame that will be returned | |
12364 | 322 $frame = Gaim::PluginPref::Frame->new(); |
11278 | 323 |
12364 | 324 # Create a new boolean option with a label "Boolean Label" and then add |
325 # it to the frame | |
326 $ppref = Gaim::Pref->new_with_label("Boolean Label"); | |
327 $frame->add($ppref); | |
11278 | 328 |
12364 | 329 $ppref = Gaim::PluginPref->new_with_name_and_label( |
330 "/plugins/core/perl_test/bool", "Boolean Preference"); | |
331 $frame->add($ppref); | |
11278 | 332 |
12364 | 333 # Create a set of choices. To do so we must set the type to 1 which is |
334 # the numerical equivelant of the GaimPrefType for choice. | |
335 $ppref = Gaim::PluginPref->new_with_name_and_label( | |
336 "/plugins/core/perl_test/choice", "Choice Preference"); | |
337 $ppref->set_type(1); | |
338 $ppref->add_choice("ch0", $frame); | |
11278 | 339 # The following will be the default value as set from plugin_load |
12364 | 340 $ppref->add_choice("ch1", $frame); |
341 $frame->add($ppref); | |
11278 | 342 |
12364 | 343 # Create a text box. The default value will be "Foobar" as set by |
344 # plugin_load | |
345 $ppref = Gaim::PluginPref->new_with_name_and_label( | |
346 "/plugins/core/perl_test/text", "Text Box Preference"); | |
347 $ppref->set_max_length(16); | |
348 $frame->add($ppref); | |
6598 | 349 |
11278 | 350 return $frame; |
351 } | |
352 @endcode | |
353 | |
11377 | 354 Using the Gtk2-Perl module for Perl it is possible to create tailored @c GtkFrame elements and display them in a preference window. Note that Gtk2-Perl must be loaded with @c require and not @c use . 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. |
11278 | 355 |
356 @code | |
357 %PLUGIN_INFO = { | |
358 ..., | |
359 # Used to differentiate between a regular and a Gtk preference frame | |
360 GTK_UI => TRUE, | |
361 gtk_prefs_info => "gtk_prefs_info_cb", | |
362 } | |
363 @endcode | |
364 | |
365 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 | 366 |
11278 | 367 @code |
368 # A simple call back that prints out whatever value it is given as an argument. | |
369 sub button_cb { | |
370 my $widget = shift; | |
371 my $data = shift; | |
372 print "Clicked button with message: " . $data . "\n"; | |
373 } | |
374 | |
375 sub gtk_prefs_info_cb { | |
11377 | 376 # Create a button that prints a message to the console and places it in the frame. |
377 use Glib; | |
378 require Gtk2; | |
11278 | 379 |
11377 | 380 $frame = Gtk2::Frame->new(\'Gtk Test Frame\'); |
381 $button = Gtk2::Button->new(\'Print Message\'); | |
11278 | 382 |
11377 | 383 $frame->set_border_width(10); |
384 $button->set_border_width(150); | |
385 $button->signal_connect("clicked" => \&button_cb, "Message Text"); | |
386 $frame->add($button); | |
11278 | 387 |
11377 | 388 $button->show(); |
389 $frame->show(); | |
390 | |
11278 | 391 return $frame; |
392 } | |
393 @endcode | |
6598 | 394 |
11278 | 395 @section request-api Request Dialog Box API |
396 | |
397 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. | |
398 | |
399 These arguments are the same for each of the three request types: | |
400 @arg @em handle - The plugin handle. | |
401 @arg @em title - String title for the dialog. | |
402 @arg @em primary - The first sub-heading. | |
403 @arg @em secondary - The second sub-heading. | |
404 @arg @em ok_text - The Text for the OK button. | |
405 @arg @em ok_cb - The string name of the perl subroutine to call when the OK button is clicked. | |
406 @arg @em cancel_text - The text for the Cancel button. | |
407 @arg @em cancel_cb - The string name of the perl subroutine to call when the Cancel button is clicked. | |
408 @arg @em default_value - Default text string to display in the input box. | |
409 @arg @em multiline - Boolean where true indicates multiple line input boxes are allowed. | |
410 @arg @em masked - Boolean indicating if the user can edit the text. | |
411 @arg @em hint - See source for more information - can be left blank. | |
412 @arg @em filename - String defualt file name value. | |
413 @arg @em savedialog - Boolean where true indicates use as a save file dialog and false indicates an open file dialog. | |
6598 | 414 |
11278 | 415 @code |
416 # Create a simple text input box | |
417 Gaim::Request::input(handle, title, primary, secondary, default_value, multiline, masked, hint, ok_text, ok_cb, cancel_text, cancel_cb); | |
418 | |
419 # Propt user to select a file | |
420 Gaim::Request::file(handle, title, filename, savedialog, ok_cb, cancel_cb); | |
421 | |
422 # Create a unique input dialog as shown in the following example | |
423 Gaim::Request::fields(handle, title, primary, secondary, fields, ok_text, ok_cb, cancel_text, cancel_cb); | |
424 @endcode | |
425 | |
426 What follows is an example of a @c Gaim::Request::fields() dialog box with two callbacks for an OK button and a Cancel Button. | |
427 | |
428 @code | |
429 sub ok_cb_test{ | |
430 # The $fields is passed to the callback function when the button is clicked. | |
431 # To get the values they must be extracted from $fields by name. | |
432 $fields = shift; | |
433 $account = Gaim::Request::fields_get_account($fields, "acct_test"); | |
434 $int = Gaim::Request::fields_get_integer($fields, "int_test"); | |
435 $choice = Gaim::Request::fields_get_choice($fields, "ch_test"); | |
436 } | |
437 | |
438 sub cancel_cb_test{ | |
439 # 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
|
440 } |
6598 | 441 |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
442 sub plugin_load { |
11278 | 443 my $plugin = shift; |
6598 | 444 |
11278 | 445 # Create a group to pool together mutltiple fields. |
446 $group = Gaim::Request::field_group_new("Group Name"); | |
447 | |
448 # Each fields is created with Gaim::Request::*_new(), is made viewable with Gaim::Request::field_*_set_show_all() | |
449 # and is then added to the group with Gaim::Request::field_group_add_field() | |
6598 | 450 |
11278 | 451 # Add an account drop down list showing all active accounts |
452 $field = Gaim::Request::field_account_new("acct_test", "Account Text", undef); | |
453 Gaim::Request::field_account_set_show_all($field, 0); | |
454 Gaim::Request::field_group_add_field($group, $field); | |
6598 | 455 |
11278 | 456 # Add an integer input box |
457 $field = Gaim::Request::field_int_new("int_test", "Integer Text", 33); | |
458 Gaim::Request::field_group_add_field($group, $field); | |
6598 | 459 |
11278 | 460 # Add a list of choices |
461 $field = Gaim::Request::field_choice_new("ch_test", "Choice Text", 1); | |
462 Gaim::Request::field_choice_add($field, "Choice 0"); | |
463 Gaim::Request::field_choice_add($field, "Choice 1"); | |
464 Gaim::Request::field_choice_add($field, "Choice 2"); | |
6598 | 465 |
11278 | 466 Gaim::Request::field_group_add_field($group, $field); |
467 | |
468 # Create a Gaim::Request and add the group that was just created. | |
469 $request = Gaim::Request::fields_new(); | |
470 Gaim::Request::fields_add_group($request, $group); | |
6598 | 471 |
11278 | 472 # Display the dialog box with the input fields added earlier with the appropriate titles. |
473 Gaim::Request::fields( | |
474 $plugin, | |
475 "Request Title!", | |
476 "Primary Title", | |
477 "Secondary Title", | |
478 $request, | |
479 "Ok Text", "ok_cb_test", | |
480 "Cancel Text", "cancel_cb_test"); | |
481 } | |
482 @endcode | |
6598 | 483 |
11278 | 484 @section timeout-cb Misc: Plugin Actions, Timeouts and Callbacks |
6598 | 485 |
13351
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
486 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 Name]". 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. |
11278 | 487 |
13351
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
488 Plugin actions require the @c \%PLUGIN_INFO hash to have a key/value pair added and a callback perl subroutine defined to list the available actions, as well as one callback subroutine for each action. Also, a @c \%plugin_actions hash needs to be defined relating the action names returned by the callback to the appropriate callback subroutines for each action. |
6598 | 489 |
11278 | 490 @code |
491 %PLUGIN_INFO = { | |
492 ..., | |
13351
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
493 plugin_action_sub => "plugin_actions_cb", |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
494 } |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
495 |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
496 sub plugin_actions_cb { |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
497 my @actions = ("Action 1", "Action 2"); |
11278 | 498 } |
6598 | 499 |
13351
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
500 %plugin_actions = ( |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
501 # The callback subroutine that is called when "Tools -> Plugin -> Action 1" is selected |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
502 "Action 1" => \&action1_cb, |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
503 # The callback subroutine that is called when "Tools -> Plugin -> Action 2" is selected |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
504 "Action 2" => \&action2_cb |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
505 ); |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
506 |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
507 sub action1_cb { |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
508 Gaim::debug_info("Test Plugin", "Action 1 activated"); |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
509 } |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
510 |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
511 sub action2_cb { |
967dd29cc4ae
[gaim-migrate @ 15723]
Richard Laager <rlaager@wiktel.com>
parents:
12364
diff
changeset
|
512 Gaim::debug_info("Test Plugin", "Action 2 activated"); |
11278 | 513 } |
514 @endcode | |
6598 | 515 |
11278 | 516 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. |
517 | |
518 @code | |
519 sub timeout_cb { | |
520 my $plugin = shift; | |
14692 | 521 print "Timeout occurred."; |
11278 | 522 |
523 # Reschedule timeout | |
524 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin); | |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
525 } |
6603
21084026030b
[gaim-migrate @ 7127]
Christian Hammond <chipx86@chipx86.com>
parents:
6602
diff
changeset
|
526 |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
527 sub plugin_load { |
11278 | 528 $plugin = shift; |
529 | |
530 # Schedule a timeout for ten seconds from now | |
531 Gaim::timeout_add($plugin, 10, \&timeout_cb, $plugin); | |
532 } | |
533 @endcode | |
6598 | 534 |
11278 | 535 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. |
536 | |
537 @code | |
538 sub signal_cb { | |
539 # The handle and the user data come in as arguments | |
540 my ($handle, $data) = @_; | |
541 print "User just connected."; | |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
542 } |
6598 | 543 |
11278 | 544 sub plugin_load { |
545 $plugin = shift; | |
546 | |
547 # User data to be given as an argument to the callback perl subroutine. | |
548 $data = ""; | |
6598 | 549 |
11278 | 550 # A pointer to the actual plugin handle needed by the callback function |
551 $plugin_handle = Gaim::Accounts::get_handle(); | |
6598 | 552 |
11278 | 553 # Connect the perl subroutine 'signal_cb' to the event 'account-connecting' |
554 Gaim::signal_connect($plugin_handle, "account-connecting", $plugin, \&signal_cb, $data); | |
555 } | |
556 @endcode | |
557 | |
558 @section Resources | |
559 @see API Documentation | |
6602
76683fe3c96d
[gaim-migrate @ 7126]
Christian Hammond <chipx86@chipx86.com>
parents:
6598
diff
changeset
|
560 |
6598 | 561 */ |