Tải bản đầy đủ (.pdf) (393 trang)

open source messaging application development

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (18.59 MB, 393 trang )

Open Source Messaging
Application Development:
Building and Extending Gaim
SEAN EGAN
Egan_467-3Front.fm Page i Monday, June 27, 2005 11:00 AM
Open Source Messaging Application Development: Building and Extending Gaim
Copyright © 2005 by Sean Egan
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN (pbk): 1-59059-467-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Jason Gilmore
Technical Reviewer: Nathan Walp
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Associate Publisher: Grace Wong
Project Manager: Beth Christmas
Copy Edit Manager: Nicole LeClerc
Copy Editor: Candace English
Production Manager: Kari Brooks-Copony
Production Editor: Kelly Winquist
Compositor: Susan Glinert and Wordstop Technologies Pvt. Ltd., Chennai
Proofreader: Linda Seifert
Indexer: Broccoli Information Services
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames


Manufacturing Manager: Tom Debolski
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th
Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co. KG,
Tiergartenstr. 17, 69112 Heidelberg, Germany.
In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or visit
. Outside the United States: fax +49 6221 345229, e-mail ,
or visit .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit .
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any
liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly
or indirectly by the information contained in this work.
The source code for this book is available to readers at in the Downloads section.
Egan_467-3Front.fm Page ii Monday, June 27, 2005 11:00 AM
For Dad
Egan_467-3Front.fm Page iii Monday, June 27, 2005 11:00 AM
Egan_467-3Front.fm Page iv Monday, June 27, 2005 11:00 AM
v
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
CHAPTER 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 2 The Open Source Development Process . . . . . . . . . . . . . . . . . . . . . . 23
CHAPTER 3 Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
CHAPTER 4 Programming Gaim. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
CHAPTER 5 GTK+ Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
CHAPTER 6 Advanced GTK+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

CHAPTER 7 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
CHAPTER 8 Protocol Plug-Ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
CHAPTER 9 Internationalization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
CHAPTER 10 Portability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
INDEX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Egan_467-3Front.fm Page v Monday, June 27, 2005 11:00 AM
Egan_467-3Front.fm Page vi Monday, June 27, 2005 11:00 AM
vii
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
■CHAPTER 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
History of IM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
History of Gaim. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Setting Up Your Build Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Cygwin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
MinGW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Library Dependencies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Getting Gaim. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Configuring Gaim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Compiling Gaim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Using Gaim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Adding a New Account. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Buddy List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Conversations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
■CHAPTER 2 The Open Source Development Process . . . . . . . . . . . . . . . . . . 23
Open Source versus Free Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Why the Open Source Development Process Works . . . . . . . . . . . . . . . . . 24
Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Motivations of Open Source Developers . . . . . . . . . . . . . . . . . . . . . . 27
How the Open Source Development Process Works . . . . . . . . . . . . . . . . . 29
Meritocracy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Cathedral and the Bazaar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Contents
Egan_467-3Front.fm Page vii Monday, June 27, 2005 11:00 AM
viii
■CONTENTS
Contributing to Open Source Development. . . . . . . . . . . . . . . . . . . . . . . . . 31
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Climbing the Ranks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Managing Your Own Open Source Project . . . . . . . . . . . . . . . . . . . . . . . . . 34
Becoming a Maintainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Management Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
■CHAPTER 3 Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Emacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
vi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
How a Program Is Built. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Invoking GCC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Makefile Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

Implicit Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Multiple Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Autotools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Automake. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Autoconf. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Other Required Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
autogen.sh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Using the Build Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
GDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Running Your Application Within GDB . . . . . . . . . . . . . . . . . . . . . . . . 61
Analyzing Core Dumps with GDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Debugging Segfaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
CVS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Using CVS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
ViewCVS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
SourceForge.net. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Submitting a Tracker Item. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Managing Tracker Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Egan_467-3Front.fm Page viii Monday, June 27, 2005 11:00 AM
■CONTENTS
ix
■CHAPTER 4 Programming Gaim. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Compiling Your First Plug-In: helloworld.c. . . . . . . . . . . . . . . . . . . . . . 85
Anatomy of a Gaim Plug-in. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
GAIM_INIT_PLUGIN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
GaimPluginInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

plugin_load() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
#include Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Object-Oriented C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Accessor and Mutator Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Hash Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
The Gaim API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Core/UI Split. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
GaimAccount (account.h) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
GaimConnection (connection.h) . . . . . . . . . . . . . . . . . . . . . . 108
GaimConversation (conversation.h) . . . . . . . . . . . . . . . . . 109
GaimBuddyList (blist.h) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
■CHAPTER 5 GTK+ Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Overview of GTK+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Language Bindings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Anatomy of a GTK+ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
The gtk_init() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
The Main Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
User Interface Principles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

Elegance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
GNOME HIG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Egan_467-3Front.fm Page ix Monday, June 27, 2005 11:00 AM
x
■CONTENTS
Creating Dialogs in GTK+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Widget Packing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Container Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Event Listeners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
A Sample GTK+ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Gaim Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
GTK+ Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
GtkLabel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
GtkImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
GtkProgressBar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
GtkEntry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
GtkSpinButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
GtkButton. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
GtkToggleButton. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
GtkCheckButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
GtkRadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
GtkTextView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
GtkTreeView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
GtkComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
GtkToolbar. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
GtkMenu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
GtkDialog. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
GtkFileChooserDialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

A Gaim Plug-in Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Gaim Plug-in Boilerplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
The Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Using Gaim Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Scanning Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Hooking into Account Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Creating the GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
The Entire Plug-In. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Potential Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Egan_467-3Front.fm Page x Monday, June 27, 2005 11:00 AM
■CONTENTS
xi
■CHAPTER 6 Advanced GTK+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
A Sample Plug-In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
GObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Object-Oriented Features of GObject . . . . . . . . . . . . . . . . . . . . . . . . 199
Using GObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Creating a Composite Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
GdkPixbuf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Working with Image Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
GdkPixbufLoader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Animations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Pango . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
PangoLayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
PangoAttribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
GDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
The X Window System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
GdkDrawable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Drawing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

Overriding GtkWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Data Fields of GtkWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Overriding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Receiving Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
■CHAPTER 7 Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
The OSI Seven-Layer Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Domain Name Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Connecting to a Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Accepting Connections from Clients. . . . . . . . . . . . . . . . . . . . . . . . . 248
Reading and Writing Data to a Socket . . . . . . . . . . . . . . . . . . . . . . . 252
Non-blocking I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
select() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
fd_set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
timeval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Non-Blocking Sockets with select() . . . . . . . . . . . . . . . . . . . . . . 255
Integrating into GLib’s Main Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
GIOChannel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
g_io_add_watch(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Egan_467-3Front.fm Page xi Monday, June 27, 2005 11:00 AM
xii
■CONTENTS
A Sample Gaim Plug-in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
The Boilerplate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
plugin_load() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
incoming_cb() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
create_response() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
The Final Plug-In. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

Possible Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
■CHAPTER 8 Protocol Plug-Ins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Protocol Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Packet Framing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Packet Headers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Packet Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Protocol Implementation in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Binary Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Text Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Learning Protocols. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Reverse Engineering. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Legal Ramifications of Reverse Engineering . . . . . . . . . . . . . . . . . . 281
Packet Sniffing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Reverse Engineering Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Interfacing with Gaim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
server.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
GaimPluginProtocolInfo. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Command Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Program Flow of a prpl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
■CHAPTER 9 Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Internationalization Defined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Text Encoding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
ASCII. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Foreign Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
iconv. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Knowing What Encodings to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Code Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
UTF-8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Egan_467-3Front.fm Page xii Monday, June 27, 2005 11:00 AM
■CONTENTS
xiii
Translations with gettext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
An Overview of gettext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Setting Up gettext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Coding with gettext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Translating with gettext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Using Translations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
gettext Summarized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
■CHAPTER 10 Portability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
The C Programming Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
K&R, ANSI, and C99 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
libc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Tips on Writing Portable C Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
POSIX. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Porting to Windows with MinGW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Cygwin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Windows Compatibility Libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
GLib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Portable Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Utility Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
File Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Plug-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
GDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

X11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Win32. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
WIMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Egan_467-3Front.fm Page xiii Monday, June 27, 2005 11:00 AM
Egan_467-3Front.fm Page xiv Monday, June 27, 2005 11:00 AM
xv
About the Author
■SEAN EGAN maintains the Gaim project,
.
He is
a major contributor to the project, managing a team of 10 core developers,
and reviewing and incorporating patches from over 200 contributors. A
Long Island, NY native, Sean has a bachelor’s degree in computer science
from Binghamton Universtiy.
Sean has been involved in Gaim development for five years and learned
most of what he knows about software development from his experience
with the project.
Egan_467-3Front.fm Page xv Monday, June 27, 2005 11:00 AM
Egan_467-3Front.fm Page xvi Monday, June 27, 2005 11:00 AM
xvii
About the Technical Reviewer
■NATHAN WALP is a software engineer at Recognition Research, Inc. in Blacksburg, VA. He has
more than five years experience writing software, and has been working on Gaim since 2001; he
is responsible for Jabber support, among many other things.
Egan_467-3Front.fm Page xvii Monday, June 27, 2005 11:00 AM
Egan_467-3Front.fm Page xviii Monday, June 27, 2005 11:00 AM
xix
Acknowledgments

I’d like to thank my friends and family for their support while I was writing this book, especially
my parents Doug and Lynn, and my sister Erin.
I’d like to thank everyone at Apress for their support and patience with me, especially
those I worked with directly: Beth Christmas, Jason Gilmore, Candace English, Julie Miller,
and Kelly Winquist.
Mostly, I’d like to thank everyone who’s contributed to Gaim, making this book possible;
especially Nathan Walp (who is also the technical reviewer for this book), Mark Spencer,
Jim Duchek, Rob Flynn, Syd Logan, Eric Warmenhoven, Adam Fritzler, Benjamin Miller,
Decklin Foster, Mark Doliner, Luke Schierer, Ethan Blanton, Etan Reisner, Tim Ringenbach,
Daniel Atallah, Robert McQueen, Christian Hammond, Herman Bloggs, Stu Tomlinson,
Gary Kramlich, Ka-Hing Cheung, Kevin Stange, and Felipe Contreras.
Egan_467-3Front.fm Page xix Monday, June 27, 2005 11:00 AM
Egan_467-3Front.fm Page xx Monday, June 27, 2005 11:00 AM
xxi
Introduction
I discovered Gaim in the AOL Instant Messenger (AIM) user profile of a friend about five years
ago. I didn’t know a lot about Linux then. I knew that it was a free implementation of UNIX with
publicly available source code, but my previous experiences with UNIX comprised staring at a
shell prompt, trying to type cryptic commands into a “DOS-like” console. Likewise, I assumed
that Linux was primitive compared to the high-tech, state-of-the-art Windows 98 I was using
that day when I read my friend’s profile.
“Visit the Gaim Web page,” it invited me. I followed the link and found my preconceptions
were wrong. On what I had seen as a platform solely for Web servers and corporate mainframes,
I could perform such everyday tasks as chatting with my friends. I expected nothing but text
commands, but I found Web browsers, e-mail clients, and games—all with windows to scroll
and buttons to click. Linux no longer looked like DOS; it was an actively developed desktop
operating system. Wanting to contribute to that development process, I quickly downloaded
the Gaim source code and started coding.
At the time, though, I had just started learning how to use my operating system and didn’t
know any C (the language Gaim is written in). This didn’t stop me; I quickly taught myself all the

skills necessary to write this hugely popular desktop application, using Gaim as an example.
Today, I’m the lead developer of the Gaim project, and I work closely with a group of developers
to maintain and enhance the most popular open source instant messaging application on the
planet.
About Gaim
Gaim is a modular instant messaging client that supports a wide variety of IM protocols,
including AIM, ICQ, MSN, Yahoo!, and Jabber. Although it was originally written for Linux, it
now runs on most popular operating systems, including Windows and Mac OS X. Although
exact user statistics are impossible to obtain, its users number in the hundreds of thousands, if
not over a million. It’s so popular because, in addition to functionality available in other clients,
it offers many unique features and is infinitely extensible through a powerful plug-in API. Most
importantly in the context of this book is that Gaim is free and open source software.
Egan_467-3Front.fm Page xxi Monday, June 27, 2005 11:00 AM
xxii
■INTRODUCTION
Figure 1. A screenshot of the Gaim application
“Free software” and “open source software” are terms that differ slightly in semantics but
describe essentially the same thing. When downloading Gaim, you are offered the source code.
Having unfettered access to the source code is quite valuable for understanding how Gaim
works, and is necessary should you wish to make any modifications. The only major provision
is that you grant this same right to modify your changes. This way, Gaim benefits from
hundreds of talented developers around the world, offering their contributions back to the
community. Because Gaim is built entirely with open source tools, involving yourself in its
development is easy, and it makes an excellent example to learn from.
About This Book
This book will help you to learn the same important programming skills I learned using Gaim
as an example. I’ll explore the various techniques and technologies used by Gaim, and after
reading this book you will be able to use what you’ve learned to create your own networked,
cross-platform desktop application.
This book introduces GTK+, the library used to create Gaim’s graphical user interface. You

will understand the principles behind it, and will know enough of its API to make changes to
Gaim’s GUI and to develop interfaces for your own applications.
Egan_467-3Front.fm Page xxii Monday, June 27, 2005 11:00 AM
■INTRODUCTION
xxiii
This book will also introduce network programming. You will learn what sockets are and
how to use them to make your applications communicate over the Internet. I’ll also introduce
you to a concept known as reverse engineering, showing you how to capture and interpret
network traffic so that your application can speak otherwise-closed, proprietary protocols.
I’ll examine different ways to make sure that your applications are accessible to everyone,
regardless of what kind of computer they’re running on (portability) and what languages your
users speak (internationalization). I’ll also discuss some of the important differences between
platforms, and you will learn about how computers store text. We will look at the gettext
system, which allows your application to be translated to other languages.
Throughout, I’ll examine useful coding techniques implemented within Gaim to make
programming in C and managing a large project easier. A per-chapter breakdown of the mate-
rial covered in this book follows.
Chapter 1: Getting Started
In Chapter 1, you’ll install a build environment containing all the software and libraries you’ll
need to build Gaim and other desktop programs. I’ll also introduce you to using Gaim, sharing
some little known, undocumented features.
Chapter 2: The Open Source Development Process
As an open source application, Gaim is developed with processes unfamiliar to many people.
However, the open source development process is very powerful and is gaining in popularity.
This chapter will explain how the process works and how you can become part of it.
Chapter 3: Development Tools
Chapter 3 will introduce you to the development tools used in the development of Gaim and
other open source (and proprietary) software. I will explain how to use editors, the GCC
compiler, make, GNU Autotools, CVS, and SourceForge.net so that you will be able to
contribute to Gaim as well as start your own projects from scratch.

Chapter 4: Programming Gaim
In this chapter, I will discuss the powerful programming techniques used throughout Gaim’s
source code. I will discuss object-oriented programming and how it is implemented in C by
Gaim and the GTK+ library. I will discuss important data structures used by Gaim, and finally I
will cover the Gaim plug-in API, allowing you to write plug-ins to extend Gaim’s features.
Chapter 5: GTK+ Basics
This chapter will introduce GTK+, the library used to create Gaim’s graphical user interface.
You will learn how to create your own dialogs with commonly used widgets; how to attach
callback functions, called when the user interacts with your program; and how to manipulate
existing interfaces. I will walk you through the development of a plug-in and explain each
decision made in creating the plug-in as I make it.
Egan_467-3Front.fm Page xxiii Monday, June 27, 2005 11:00 AM
xxiv
■INTRODUCTION
Chapter 6: Advanced GTK+
Now that you’re equipped with a basic of understanding of GTK+, I will explore its internal
workings more thoroughly. You will learn how to create your own GUI objects, or widgets, by
following a sample plug-in that radically changes Gaim’s File Transfer dialog with three such
custom objects.
Chapter 7: Sockets
In Chapter 7, I’ll introduce sockets, the programmatic interface to the Internet. You will learn
some basic principles of networking, and how to communicate over a network. I will explain
how to hook your networking code to GTK+’s GLib library to integrate it into GTK+’s main loop
and ensure cross-platform compatibility.
Chapter 8: Protocol Plug-Ins
After learning how to communicate over the Internet, you will learn about various protocols,
including IM protocols. You will learn how to intercept and interpret Internet traffic used by IM
clients—a process called reverse engineering. I will then teach you how to add a new protocol
to Gaim as a protocol plug-in. I will review how Gaim and the plug-in communicate events
between each other.

Chapter 9: Internationalization
In this chapter, you will learn how to write programs that speakers of any language can use. I
will review the gettext system for providing translation of your program at runtime. I will
explain the concept of text encodings, why they are significant, and how to write code that
understands them.
Chapter 10: Portability
Chapter 10 will address portability; that is, making your program run on as many systems as
possible, regardless of hardware or operating system. I will discuss major differences you need
to concern yourself with and how to use the portability functions of GTK+.
Prerequisites
I mentioned that when I started working on Gaim I was a complete amateur who didn’t even
know the language it’s programmed in. As such, I don’t expect you to be very experienced
either. You’ll learn as we go.
However, I won’t be introducing basic concepts of C, the language used to build Gaim.
Therefore,you should possess at least a rudimentary understanding of the language, and have
a basic knowledge of underlying principles of computer programming. Although I learned C by
example, there are other books that will do a better job of teaching you This book will focus on
higher-level aspects of the development process. However, you can most likely get by with
knowing C++, Java, or another language with a C-like syntax, as I will elaborate on more-
difficult or obscure techniques.
Egan_467-3Front.fm Page xxiv Monday, June 27, 2005 11:00 AM

×