Rename jamentotypes.* to JamendoMeta.*
[amarok.git] / HACKING
blobb444117c3951dc1729a1a7242d03b39c834f6ff0
1 Hacking on Amarok
2 -----------------
4 Please respect these guidelines when coding for Amarok, thanks!
6 * Where this document isn't clear, refer to Amarok code.
9 This C++ FAQ is a life saver in many situations, so you want to keep it handy:
11   http://www.parashift.com/c++-faq-lite/
14 Formatting
15 ----------
16 * Spaces, not tabs
17 * Indentation is 4 spaces
18 * Lines should be limited to 90 characters
19 * Spaces between brackets and argument functions
20 * For pointer and reference variable declarations put a space between the type
21   and the * or & and no space before the variable name.
22 * For if, else, while and similar statements put the brackets on the next line,
23   although brackets are not needed for single statements.
24 * Function and class definitions have their brackets on separate lines
25 * A function implementation's return type is on its own line.
26 * amarok.h contains some helpful macros, foreach and foreachType.  Use them,
27   they improve coding style and readability.
29 Example:
31  | bool 
32  | MyClass::myMethod( QPtrList<QListViewItem> items, const QString &name )
33  | {
34  |     if( items.isEmpty() )
35  |         return false;
36  |
37  |     foreachType( QPtrList<QListViewItem>, items )
38  |     {
39  |         (*it)->setText( 0, name );
40  |         debug() << "Setting item name: " << name << endl;
41  |     }
42  | }
44 Header includes should be listed in the following order:
45     - Amarok includes
46     - KDE includes
47     - Qt includes
49 They should also be sorted alphabetically, for ease of locating them.  A small comment
50 if applicable is also helpful.
52 Includes in a header file should be kept to the absolute minimum, as to keep compile times
53 low. This can be achieved by using "forward declarations" instead of includes, like
54 "class QListView;" Forward declarations work for pointers and const references.
56 TIP:
57 Kate/KDevelop users can sort the headers automatically. Select the lines you want to sort,
58 then Tools -> Filter Selection Through Command -> "sort".
61 Example:
63  | #include "amarok.h"
64  | #include "debug.h"
65  | #include "playlist.h"
66  |
67  | #include "kdialogbase.h"    //baseclass
68  | #include "kpushbutton.h"    //see function...
69  |
70  | #include "qlistviewitem.h"
71  | #include "qwidget.h"
74 Comments
75 --------
76 Comment your code. Don't comment what the code does, comment on the purpose of the code. It's
77 good for others reading your code, and ultimately it's good for you too.
79 Comments are essential when adding a strange hack, like the following example:
81  | /** Due to xine-lib, we have to make KProcess close all fds, otherwise we get "device is busy" messages
82  |   * Used by AmarokProcIO and AmarokProcess, exploiting commSetupDoneC(), a virtual method that
83  |   * happens to be called in the forked process
84  |   * See bug #103750 for more information.
85  |   */
86  | class AmarokProcIO : public K3ProcIO 
87  | {
88  |     public:
89  |     virtual int commSetupDoneC() {
90  |         const int i = K3ProcIO::commSetupDoneC();
91  |         Amarok::closeOpenFiles(K3ProcIO::out[0],K3ProcIO::in[0],K3ProcIO::err[0]);
92  |         return i;
93  |     }
94  | };
97 For headers, use the Doxygen syntax. See: http://www.stack.nl/~dimitri/doxygen/
99 Example:
101  | /**
102  |  * Start playback.
103  |  * @param offset Start playing at @p msec position.
104  |  * @return True for success.
105  |  */
106  | virtual bool play( uint offset = 0 ) = 0;
109 Header Formatting
110 -----------------
111 General rules apply here.  Please keep header function definitions aligned nicely,
112 if possible.  It helps greatly when looking through the code.  Sorted methods,
113 either by name or by their function (ie, group all related methods together) is
114 great too.
117  | #ifndef AMAROK_QUEUEMANAGER_H
118  | #define AMAROK_QUEUEMANAGER_H
120  | class QueueList : public K3ListView
121  | {
122  |         Q_OBJECT
124  |     public:
125  |         Queuelist( QWidget *parent, const char *name = 0 );
126  |         ~QueueList() {};
128  |     public slots:
129  |         void    moveSelectedUp();
130  |         void    moveSelectedDown();
131  | };
133 #endif /* AMAROK_QUEUEMANAGER_H */
136 0 vs NULL
137 ---------
138 The use of 0 to express a null pointer is preferred over the use of NULL.
139 0 is not a magic value, it's the defined value of the null pointer in C++.
140 NULL, on the other hand, is a preprocessor directive (#define) and not only is
141 it more typing than '0' but preprocessor directives are less elegant.
143  |     SomeClass *instance = 0;
146 Const Correctness
147 -----------------
148 Try to keep your code const correct. Declare methods const if they don't mutate the object,
149 and use const variables. It improves safety, and also makes it easier to understand the code.
151 See: http://www.parashift.com/c++-faq-lite/const-correctness.html
154 Example:
156  | bool 
157  | MyClass::isValidFile( const QString& path ) const
158  | {
159  |     const bool valid = QFile::exist( path );
161  |     return valid;
162  | }
165 Debugging
166 ---------
167 debug.h contains some handy functions for our debug console output.
168 Please use them instead of kDebug().
170 Usage:
172  | #include "debug.h"
174  | debug()   << "Something is happening" << endl;
175  | warning() << "Something bad may happen" << endl;
176  | error()   << "Something bad did happen!" << endl;
178 Additionally, there are some macros for debugging functions:
180 DEBUG_BLOCK
181 DEBUG_FUNC_INFO
182 DEBUG_LINE_INFO
183 DEBUG_INDENT
184 DEBUG_UNINDENT
186 AMAROK_NOTIMPLEMENTED
187 AMAROK_DEPRECATED
189 threadweaver.h has two additional macros:
190 DEBUG_THREAD_FUNC_INFO outputs the memory address of the current QThread or 'none'
191     if its the original GUI thread.
192 SHOULD_BE_GUI outputs a warning message if it occurs in a thread that isn't in
193     the original "GUI Thread", otherwise it is silent. Useful for documenting
194     functions and to prevent problems in the future.
197 Usage of Amarok::config()
198 -------------------------
199 We provide this method for convenience, but it is important to use it properly. By
200 inspection, we can see that we may produce very obscure bugs in the wrong case:
202  | KConfig 
203  | *config( const QString &group )
204  | {
205  |    //Slightly more useful config() that allows setting the group simultaneously
206  |    KGlobal::config()->setGroup( group );
207  |    return KGlobal::config();
208  | }
210 Take the following example:
212  | void
213  | f1()
214  | {
215  |    KConfig *config = Amarok::config( "Group 2" );
216  |    config->writeEntry( "Group 2 Variable", true );
217  | }
219  | void
220  | doStuff()
221  | {
222  |   KConfig *config = Amarok::config( "Group 1" );
223  |   f1();
224  |   config->writeEntry( "Group 1 Variable", true );
225  | }
227 We would expect the following results:
229  | [Group 1]
230  | Group 1 Variable = true
232  | [Group 2]
233  | Group 2 Variable = true
235 However because the config group is changed before writing the entry:
236  | [Group 1]
238  | [Group 2]
239  | Group 1 Variable = true
240  | Group 2 Variable = true
242 Which is clearly incorrect. And hard to see when your wondering why f1() is not
243 working. So do not store a value of Amarok::config, make it a habit to just 
244 always call writeEntry or readEntry directly.
246 Correct:
247 | amarok::config( "Group 1" )->writeEntry( "Group 1 Variable", true );
250 Errors & Asserts
251 ----------------
252 *Never use assert() or fatal(). There must be a better option than crashing a user's
253 application (its not uncommon for end-users to have debugging enabled).
255 *KMessageBox is fine to use to prompt the user, but do not use it to display errors
256 or informational messages. Instead, KDE::StatusBar has a few handy methods. Refer to
257 amarok/src/statusbar/statusBarBase.h
260 Copyright
261 ---------
262 To comply with the GPL, add your name, email address & the year to the top of any file
263 that you edit. If you bring in code or files from elsewhere, make sure its
264 GPL-compatible and to put the authors name, email & copyright year to the top of
265 those files.
268 Thanks, now have fun!
269   -- the Amarok developers