common.h

Go to the documentation of this file.
00001 /*
00002         Author: Marco Costalba (C) 2005-2006
00003 
00004         Copyright: See COPYING file that comes with this distribution
00005 
00006 */
00007 #ifndef COMMON_H
00008 #define COMMON_H
00009 
00010 #include <qstringlist.h>
00011 #include <qvaluevector.h>
00012 #include <qdict.h>
00013 #include <qmap.h>
00014 #include <qevent.h>
00015 #include <qcolor.h>
00016 #include <qfont.h>
00017 #include <qvariant.h>
00018 
00019 /*
00020    QVariant does not support size_t type used in Qt containers, this is
00021    a problem on 64bit systems where size_t != uint and when using debug
00022    macros on size_t variables, as example dbg(vector.count()), a compile
00023    error occurs.
00024    Workaround this using a function template and a specialization.
00025    Function _valueOf() is used by debug macros
00026 */
00027 template<typename T> inline const QString _valueOf(const T& x) { return QVariant(x).toString(); }
00028 inline const QString& _valueOf(const QString& x) { return x; }
00029 inline const QString  _valueOf(size_t x) { return QString::number((uint)x); }
00030 
00031 // some debug macros
00032 #define dbg(x)    qDebug(#x " is <%s>", _valueOf(x).latin1())
00033 #define dbs(x)    qDebug(_valueOf(x))
00034 #define dbp(s, x) qDebug(QString(s).arg(_valueOf(x)))
00035 #define db1       qDebug("Mark Nr. 1")
00036 #define db2       qDebug("Mark Nr. 2")
00037 #define db3       qDebug("Mark Nr. 3")
00038 #define db4       qDebug("Mark Nr. 4")
00039 #define dbStart   dbs("Starting timer..."); QTime _t; _t.start()
00040 #define dbRestart dbp("Elapsed time is %1 ms", _t.restart())
00041 
00042 // some syntactic sugar
00043 #define FOREACH(type, i, c) for (type::const_iterator i((c).constBegin()),    \
00044                                  _e##i##_((c).constEnd()); i != _e##i##_; ++i)
00045 
00046 #define FOREACH_SL(i, c)    FOREACH(QStringList, i, c)
00047 
00048 // type shortcuts
00049 typedef const QString&        SCRef;
00050 typedef QStringList&          SList;
00051 typedef const QStringList&    SCList;
00052 typedef QValueVector<QString> StrVect;
00053 
00054 namespace QGit {
00055 
00056         // minimum git version required
00057         extern const QString GIT_VERSION;
00058 
00059         // key bindings
00060         enum KeyType {
00061                 KEY_UP,
00062                 KEY_DOWN,
00063                 SHIFT_KEY_UP,
00064                 SHIFT_KEY_DOWN,
00065                 KEY_LEFT,
00066                 KEY_RIGHT,
00067                 CTRL_PLUS,
00068                 CTRL_MINUS,
00069                 KEY_U,
00070                 KEY_D,
00071                 KEY_DELETE,
00072                 KEY_B,
00073                 KEY_BCKSPC,
00074                 KEY_SPACE,
00075                 KEY_R,
00076                 KEY_P,
00077                 KEY_F
00078         };
00079 
00080         // tab pages
00081         enum TabType {
00082                 TAB_REV,
00083                 TAB_PATCH,
00084                 TAB_FILE
00085         };
00086 
00087         // graph elements
00088         enum LaneType {
00089                 EMPTY,
00090                 ACTIVE,
00091                 NOT_ACTIVE,
00092                 MERGE_FORK,
00093                 MERGE_FORK_R,
00094                 MERGE_FORK_L,
00095                 JOIN,
00096                 JOIN_R,
00097                 JOIN_L,
00098                 HEAD,
00099                 HEAD_R,
00100                 HEAD_L,
00101                 TAIL,
00102                 TAIL_R,
00103                 TAIL_L,
00104                 CROSS,
00105                 CROSS_EMPTY,
00106                 INITIAL,
00107                 BRANCH,
00108                 UNAPPLIED,
00109                 APPLIED,
00110                 BOUNDARY,
00111                 BOUNDARY_C, // corresponds to MERGE_FORK
00112                 BOUNDARY_R, // corresponds to MERGE_FORK_R
00113                 BOUNDARY_L, // corresponds to MERGE_FORK_L
00114 
00115                 LANE_TYPES_NUM
00116         };
00117         const int COLORS_NUM = 8;
00118 
00119         // graph helpers
00120         inline bool isHead(int x) { return (x == HEAD || x == HEAD_R || x == HEAD_L); }
00121         inline bool isTail(int x) { return (x == TAIL || x == TAIL_R || x == TAIL_L); }
00122         inline bool isJoin(int x) { return (x == JOIN || x == JOIN_R || x == JOIN_L); }
00123         inline bool isFreeLane(int x) { return (x == NOT_ACTIVE || x == CROSS || isJoin(x)); }
00124         inline bool isBoundary(int x) { return (x == BOUNDARY || x == BOUNDARY_C ||
00125                                                 x == BOUNDARY_R || x == BOUNDARY_L); }
00126         inline bool isMerge(int x) { return (x == MERGE_FORK || x == MERGE_FORK_R ||
00127                                              x == MERGE_FORK_L || isBoundary(x)); }
00128         // custom events
00129         enum EventType {
00130                 ERROR_EV      = 65432,
00131                 POPUP_LIST_EV = 65433,
00132                 POPUP_FILE_EV = 65434,
00133                 POPUP_TREE_EV = 65435,
00134                 MSG_EV        = 65436,
00135                 ANN_PRG_EV    = 65437,
00136                 UPD_DM_EV     = 65438,
00137                 UPD_DM_MST_EV = 65439
00138         };
00139 
00140         // list views columns
00141         enum ColumnType {
00142                 GRAPH_COL   = 0,
00143                 ANN_ID_COL  = 1,
00144                 LOG_COL     = 2,
00145                 AUTH_COL    = 3,
00146                 TIME_COL    = 4,
00147                 COMMIT_COL  = 97, // dummy col used for sha searching
00148                 LOG_MSG_COL = 98, // dummy col used for log messages searching
00149                 SHA_MAP_COL = 99  // dummy col used when filter output is a set of matching sha
00150         };
00151 
00152         inline bool isInfoCol(int x) { return (x == TIME_COL || x == LOG_COL || x == AUTH_COL); }
00153 
00154         // default list view widths
00155         const int DEF_GRAPH_COL_WIDTH = 80;
00156         const int DEF_LOG_COL_WIDTH   = 500;
00157         const int DEF_AUTH_COL_WIDTH  = 230;
00158         const int DEF_TIME_COL_WIDTH  = 130;
00159 
00160         // colors
00161         extern const QColor BROWN;
00162         extern const QColor ORANGE;
00163         extern const QColor DARK_ORANGE;
00164         extern const QColor LIGHT_ORANGE;
00165         extern const QColor LIGHT_BLUE;
00166         extern const QColor PURPLE;
00167         extern const QColor DARK_GREEN;
00168 
00169         // initialized at startup according to system wide settings
00170         extern QColor ODD_LINE_COL;
00171         extern QColor EVEN_LINE_COL;
00172         extern QFont  TYPE_WRITER_FONT;
00173 
00174         // patches drag and drop
00175         extern const QString PATCHES_DIR;
00176         extern const QString PATCHES_NAME;
00177 
00178         // git index parameters
00179         extern const QString ZERO_SHA;
00180         extern const QString CUSTOM_SHA;
00181         extern const QString ALL_MERGE_FILES;
00182 
00183         // settings keys
00184         extern const QString APP_KEY;
00185         extern const QString FP_DIR_KEY;
00186         extern const QString FPATCH_ARGS_KEY;
00187         extern const QString FLAGS_KEY;
00188         extern const QString CMT_GEOM_KEY;
00189         extern const QString CMT_SPLIT_KEY;
00190         extern const QString CMT_TEMPL_KEY;
00191         extern const QString CMT_ARGS_KEY;
00192         extern const QString EX_KEY;
00193         extern const QString EX_PER_DIR_KEY;
00194         extern const QString EXT_DIFF_KEY;
00195         extern const QString REC_REP_KEY;
00196         extern const QString MCR_NAME_KEY;
00197         extern const QString MCR_TEXT_KEY;
00198         extern const QString MCR_LIST_KEY;
00199         extern const QString FONT_KEY;
00200 
00201         // settings default values
00202         extern const QString CMT_GEOM_DEF;
00203         extern const QString CMT_SPLIT_DEF;
00204         extern const QString CMT_TEMPL_DEF;
00205         extern const QString EX_DEF;
00206         extern const QString EX_PER_DIR_DEF;
00207         extern const QString EXT_DIFF_DEF;
00208         extern const QString MCR_NAME_DEF;
00209 
00210         // settings booleans
00211         enum FlagType {
00212                 // removed obsolete option
00213                 MCR_REFRESH_F   = 2,
00214                 NUMBERS_F       = 4,
00215                 // removed obsolete option
00216                 MCR_CMD_LINE_F  = 16,
00217                 DIFF_INDEX_F    = 32,
00218                 SIGN_PATCH_F    = 64,
00219                 SIGN_CMT_F      = 128,
00220                 VERIFY_CMT_F    = 256,
00221                 // removed obsolete option
00222                 REL_DATE_F      = 1024,
00223                 ALL_BRANCHES_F  = 2048,
00224                 WHOLE_HISTORY_F = 4096,
00225                 RANGE_SELECT_F  = 8192
00226         };
00227         const int FLAGS_DEF = 8512;
00228 
00229         // settings helpers
00230         uint flags(SCRef group = "");
00231         bool testFlag(uint f, SCRef group = "");
00232         void setFlag(uint f, bool b, SCRef group = "");
00233         void writeSetting(SCRef key, SCRef value, SCRef group = "");
00234 
00235         // misc helpers
00236         bool stripPartialParaghraps(const QByteArray& src, QString* dst, QString* prev);
00237         bool writeToFile(SCRef fileName, SCRef data, bool setExecutable = false);
00238         bool writeToFile(SCRef fileName, const QByteArray& data, bool setExecutable = false);
00239         bool readFromFile(SCRef fileName, QString& data);
00240         void baAppend(QByteArray** src, const char* ascii, int len);
00241         void baAppend(QByteArray& ba, const QByteArray& data);
00242 
00243         // cache file
00244         const uint C_MAGIC  = 0xA0B0C0D0;
00245         const int C_VERSION = 13;
00246 
00247         extern const QString BAK_EXT;
00248         extern const QString C_DAT_FILE;
00249 
00250         // misc
00251         const int MAX_DICT_SIZE    = 100003; // must be a prime number see QDict docs
00252         const int MAX_MENU_ENTRIES = 15;
00253         const int MAX_RECENT_REPOS = 7;
00254         extern const QString QUOTE_CHAR;
00255 }
00256 
00257 class Rev {
00258         // prevent implicit C++ compiler defaults
00259         Rev();
00260         Rev(const Rev&);
00261         Rev& operator=(const Rev&);
00262 public:
00263         Rev(const QByteArray& b, uint s, int idx, int* next)
00264             : orderIdx(idx), ba(b), start(s) {
00265 
00266                 isDiffCache = isApplied = isUnApplied = false;
00267                 descRefsMaster = ancRefsMaster = descBrnMaster = -1;
00268                 *next = indexData();
00269         }
00270         bool isBoundary() const { return boundary; }
00271         uint parentsCount() const { return parentsCnt; }
00272         const QString parent(int idx) const;
00273         const QStringList parents() const;
00274         const QString sha() const { return mid(start + startOfs, 40); }
00275         const QString author() const { return mid(autStart, autLen); }
00276         const QString authorDate() const { return mid(autDateStart, autDateLen); }
00277         const QString shortLog() const { return mid(sLogStart, sLogLen); }
00278         const QString longLog() const { return mid(lLogStart, lLogLen); }
00279 
00280         QValueVector<int> lanes, childs;
00281         bool isDiffCache, isApplied, isUnApplied;
00282         int orderIdx;
00283         QValueVector<int> descRefs;     // list of descendant refs index, normally tags
00284         QValueVector<int> ancRefs;      // list of ancestor refs index, normally tags
00285         QValueVector<int> descBranches; // list of descendant branches index
00286         int descRefsMaster; // in case of many Rev have the same descRefs, ancRefs or
00287         int ancRefsMaster;  // descBranches these are stored only once in a Rev pointed
00288         int descBrnMaster;  // by corresponding index xxxMaster
00289 
00290 private:
00291         int indexData();
00292         const QString mid(int start, int len) const;
00293 
00294         const QByteArray& ba; // reference here!
00295         const uint start;
00296         bool boundary;
00297         uint parentsCnt, startOfs;
00298         int autStart, autLen, autDateStart, autDateLen;
00299         int sLogStart, sLogLen, lLogStart, lLogLen;
00300 };
00301 typedef QDict<Rev> RevMap; // faster then a map
00302 
00303 class RevFile {
00304 
00305         friend class Cache; // to directly load status
00306         friend class Git;
00307 
00308         // Status information is splitted in a flags vector and in a string
00309         // vector in 'status' are stored flags according to the info returned
00310         // by 'git diff-tree' without -C option.
00311         // In case of a working dir file an IN_INDEX flag is or-ed togheter in
00312         // case file is present in git index.
00313         // If file is renamed or copied an entry in 'extStatus' stores the
00314         // value returned by 'git diff-tree -C' plus source and destination
00315         // files info.
00316         // When status of all the files is 'modified' then onlyModified is
00317         // set, this let us to do some optimization in this common case
00318         bool onlyModified;
00319         QValueVector<int> status;
00320         QValueVector<QString> extStatus;
00321 
00322         // prevent implicit C++ compiler defaults
00323         RevFile(const RevFile&);
00324         RevFile& operator=(const RevFile&);
00325 public:
00326         enum StatusFlag {
00327                 MODIFIED = 1,
00328                 DELETED  = 2,
00329                 NEW      = 4,
00330                 RENAMED  = 8,
00331                 COPIED   = 16,
00332                 UNKNOWN  = 32,
00333                 IN_INDEX = 64,
00334                 ANY      = 127
00335         };
00336 
00337         RevFile() : onlyModified(true) {}
00338         QValueVector<int> dirs; // index of a string vector
00339         QValueVector<int> names;
00340         QValueVector<int> mergeParent;
00341 
00342         // helper functions
00343         int count() const { return dirs.count(); }
00344         bool statusCmp(int idx, StatusFlag sf) const {
00345 
00346                 return ((onlyModified ? MODIFIED : status.at(idx)) & sf);
00347         }
00348         const QString extendedStatus(uint idx) const {
00349         /*
00350            rf.extStatus has size equal to position of latest copied/renamed file,
00351            that could be lower then count(), so we have to explicitly check for
00352            an out of bound condition.
00353         */
00354                 return (!extStatus.isEmpty() && idx < extStatus.count() ? extStatus.at(idx) : "");
00355         }
00356 };
00357 typedef QDict<RevFile> RevFileMap;
00358 
00359 class FileAnnotation {
00360 public:
00361         explicit FileAnnotation(int id) : isValid(false), annId(id) {}
00362         FileAnnotation() : isValid(false) {}
00363         QStringList lines;
00364         bool isValid;
00365         int annId;
00366         QString fileSha;
00367 };
00368 
00369 class BaseEvent: public QCustomEvent {
00370 public:
00371         BaseEvent(SCRef d, int id) : QCustomEvent(id), payLoad(d) {}
00372         const QString myData() const { return payLoad; }
00373 private:
00374         const QString payLoad; // passed by copy
00375 };
00376 
00377 #define DEF_EVENT(X, T) class X : public BaseEvent { public:        \
00378                         explicit X (SCRef d) : BaseEvent(d, T) {} }
00379 
00380 DEF_EVENT(MessageEvent, QGit::MSG_EV);
00381 DEF_EVENT(AnnotateProgressEvent, QGit::ANN_PRG_EV);
00382 
00383 class DeferredPopupEvent : public BaseEvent {
00384 public:
00385         DeferredPopupEvent(SCRef msg, int type) : BaseEvent(msg, type) {}
00386 };
00387 
00388 class MainExecErrorEvent : public BaseEvent {
00389 public:
00390         MainExecErrorEvent(SCRef c, SCRef e) : BaseEvent("", QGit::ERROR_EV), cmd(c), err(e) {}
00391         const QString command() const { return cmd; }
00392         const QString report() const { return err; }
00393 private:
00394         const QString cmd, err;
00395 };
00396 
00397 #endif

Generated on Fri Dec 7 21:57:36 2007 for QGit by  doxygen 1.5.3