"Fossies" - the Fresh Open Source Software Archive

Member "ical-tcl/calendar/item.h" (15 Apr 2019, 8662 Bytes) of package /linux/privat/ical-3.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "item.h" see the Fossies "Dox" file reference documentation.

    1 /* Copyright (c) 1993 by Sanjay Ghemawat */
    2 
    3 #ifndef _ITEMH
    4 #define _ITEMH
    5 
    6 #include <string.h>
    7 
    8 #include "Date.h"
    9 
   10 #include "dateset.h"
   11 #include "smallintset.h"
   12 #include "misc.h"
   13 
   14 class Lexer;
   15 class charArray;
   16 class intArray;
   17 class OptionMap;
   18 
   19 class Notice;
   20 class Appointment;
   21 
   22 /*
   23  * Item
   24  *
   25  * Abstract components.
   26  *
   27  * Text: string                 associated text
   28  * Dates: DateSet               occurrence dates
   29  * RemindStart: int             No. of days before item when reminder begins.
   30  * Owner: string                User id of person who last changed the item
   31  * Uid: string                  Unique id for item
   32  * Hilite: string               Hilite mode
   33  * Todo: bool                   Is Item a to-do item
   34  * Done: bool                   Has item been marked "done"
   35  *
   36  * AsNotice: Notice*            Typecast to Notice if legal.  Null otherwise.
   37  * AsAppointment: Appointment*  Typecast to Appointment if legal ... 
   38  *
   39  */
   40 
   41 class Item {
   42   public:
   43     Item();
   44     virtual ~Item();
   45 
   46     virtual int Read(Lexer*);
   47     virtual int Parse(Lexer*, char const* keyword);
   48     virtual void Write(charArray*) const;
   49 
   50     virtual Item* Clone() const = 0;
   51 
   52     const char* GetText() const { return text; }
   53     void SetText(const char*s) { delete [] text; text = copy_string(s); }
   54 
   55     // DateSet interface
   56     virtual int contains(Date d) const;
   57     int repeats() const { return date->repeats(); }
   58     int empty() const { return date->empty(); }
   59     DateSet::RepeatType repeat_type() const { return date->type(); }
   60     void describe(charArray* buffer) const { date->describe(buffer); }
   61     void describe_terse(charArray* buffer) const {date->describe_terse(buffer); }
   62 
   63     int first(Date& result) const;
   64     int next(Date d, Date& result) const;
   65     int range(Date&, Date&) const;
   66 
   67     void set_empty() { date->set_empty(); }
   68     void set_date(Date d) { date->set_date(d); }
   69     void set_day_based_repeat(int interval, Date anchor)
   70     { date->set_day_based_repeat(interval, anchor); }
   71 
   72     void set_month_based_repeat(int interval, Date anchor)
   73     { date->set_month_based_repeat(interval, anchor); }
   74     void set_monthly_by_days(int c, int i, Date anchor, int back)
   75     { date->set_monthly_by_days(c, i, anchor, back); }
   76     void set_monthly_by_workdays(int c, int i, Date anchor, int back)
   77     { date->set_monthly_by_workdays(c, i, anchor, back); }
   78     void set_monthly_by_weeks(int c, WeekDay w, int i, Date anchor, int back)
   79     { date->set_monthly_by_weeks(c, w, i, anchor, back); }
   80 
   81     void set_month_set(SmallIntSet days, SmallIntSet months)
   82     { date->set_month_set(days, months); }
   83     void set_week_set(SmallIntSet days, SmallIntSet months)
   84     { date->set_week_set(days, months); }
   85 
   86     void set_start(Date start);
   87     void set_finish(Date finish);
   88 
   89     void delete_occurrence(Date d);
   90 
   91     int GetRemindStart() const { return remindStart; }
   92     void SetRemindStart(int r) { remindStart = r; }
   93 
   94     const char* GetOwner() const { return owner; }
   95     // effects - Return the owner.  The returned string is guaranteed
   96     //           to remain valid until the item is modified or deleted.
   97 
   98     void SetOwner(char const* o) { delete [] owner; owner = copy_string(o); }
   99     // modifies - "this"
  100     // effects  - Make "o" the owner of the item.
  101 
  102     void MakeOwner() { SetOwner(my_name()); }
  103     // modifies - this
  104     // effects  - Make the current user the owner of this item.
  105 
  106     int IsMine() const { return strcmp(my_name(), owner) == 0; }
  107     // effects  - Returns true iff this item is owned by the current user.
  108 
  109     char const* GetUid() const { return uid; }
  110 
  111     // effects - Return the uid.  The returned string is guaranteed
  112     //           to remain valid until the item is deleted.
  113 
  114     //void SetUid(char const*);
  115     // modifies - this
  116     // effects  - Sets uid to specified value.
  117 
  118     int IsUidPersistent() const { return uid_persistent; }
  119     // effects - Return true iff uid is also stored persistently
  120 
  121     virtual Notice* AsNotice() { return 0; }
  122     virtual Appointment* AsAppointment() { return 0; }
  123 
  124     /* Mark deleted */
  125     int Deleted() { return deleted; }
  126     void MarkDeleted() { deleted = 1; }
  127 
  128     /* Hilite mode */
  129     char const* Hilite() const { return hilite; }
  130     void Hilite(char const* s) { delete [] hilite; hilite = copy_string(s); }
  131 
  132     /* Todo? */
  133     int IsTodo() const { return todo; }
  134     void SetTodo(int t) { todo = t; if (!t) done = 0; }
  135 
  136     /* Done? */
  137     int IsDone() const { return done; }
  138     void SetDone(int d) { done = todo && d; }
  139 
  140     /* Options */
  141     char const* GetOption(char const* key) const;
  142     // effects  - Return value associated with option named "key".
  143     //            Returns 0 if option is not found.
  144 
  145     void SetOption(char const* key, char const* value);
  146     // modifies - this
  147     // effects  - add "<key, value>" to option list.
  148 
  149     void RemoveOption(char const* key);
  150     // modifies - this
  151     // effects  - Remove any option associated with "key"
  152 
  153     /* Comparison */
  154     int similar(Item const* x) const;
  155     // effects  Returns true iff this has same contents as x.
  156 
  157     static const int defaultRemindStart;
  158 
  159   protected:
  160     char*       text;
  161     char*       owner;
  162     char*       uid;
  163     int         uid_persistent;
  164     int         remindStart;    /* Start reminding early */
  165     int         deleted;
  166     DateSet*    date;
  167     char*       hilite;
  168     int         todo;
  169     int         done;
  170     OptionMap*  options;
  171 
  172     void CopyTo(Item*) const;
  173 };
  174 
  175 /*
  176  * Notice
  177  *
  178  * An Item with just a length.
  179  * The length is just an indication of how big a window the notice must
  180  * be displayed in.  The length field can be interpreted just as the length
  181  * field for Appointments, and if notice->length == appt->length, then
  182  * the notice will be displayed the same size as the appt.
  183  */
  184 class Notice : public Item {
  185   public:
  186     Notice()  {}
  187     ~Notice() {}
  188 
  189     virtual Item* Clone() const;
  190 
  191     virtual Notice* AsNotice() { return this; }
  192 };
  193 
  194 /*
  195  * Appointment
  196  *
  197  * An Item with a start time and a length.
  198  * Both start and length are in minutes.
  199  * Start is measured from midnight.
  200  */
  201 class Appointment : public Item {
  202   public:
  203     Appointment() : start(30), length(30), alarms(0)
  204     { timezone=copy_string("<Local>"); cache.invalidate(); }
  205 
  206     ~Appointment() {
  207         delete [] timezone;
  208         if (alarms != 0) delete alarms;
  209     }
  210 
  211     virtual int Parse(Lexer*, char const* keyword);
  212     virtual void Write(charArray*) const;
  213 
  214     virtual Item* Clone() const;
  215 
  216     virtual int contains(Date d) const;
  217 
  218     int GetStart(int d) const { return d < 0 ? start : time_from_tz(Date(d), start); }
  219     void SetStart(int d, int s) { start = d < 0 ? s : time_to_tz(Date(d), s); }
  220 
  221     int GetLength() const { return length; }
  222     void SetLength(int l) { length = l; }
  223 
  224     const char* GetTimezone() const { return timezone; }
  225     void SetTimezone(const char*tz, bool convert_remote) {
  226         Date d;
  227         cache.invalidate();
  228         if (convert_remote && date->first(d))
  229             start=time_from_tz(d,start);
  230         else 
  231             convert_remote=false;
  232         delete [] timezone;
  233         timezone = copy_string(tz);
  234         if (convert_remote) start=time_to_tz(d,start);
  235     }
  236 
  237     intArray* GetAlarms() const { return alarms; }
  238     // effects - Return array of alarm times.  If no alarm times
  239     //           have been set for this item, return NULL.
  240     //           The returned array is guaranteed to remain
  241     //           valid until the item is modified or deleted.
  242 
  243     void SetAlarms(intArray* list) {
  244         if (alarms == 0) alarms = new intArray;
  245         alarms->clear();
  246         *alarms = *list;
  247     }
  248     // modifies - this
  249     // effects  - Sets alarms for this item from the contents of list.
  250 
  251     virtual Appointment* AsAppointment() { return this; }
  252 
  253   private:
  254     int start;
  255     int length;
  256     intArray* alarms;           /* Alarm times */
  257     const char *timezone;
  258 
  259     bool has_timezone() const { return timezone[0] != '<'; }
  260     int time_from_tz(Date d, int t) const
  261     { if (has_timezone()) convert_tz(d, t, false); return t; }
  262     int time_to_tz(Date d, int t) const
  263     { if (has_timezone()) convert_tz(d, t, true); return t; }
  264     void datetime_to_tz(Date &d, int &t) const
  265     { if (has_timezone()) convert_tz(d, t, true); }
  266 
  267     void convert_tz(Date &d, int &min, bool to_tz) const;
  268     /*
  269      * tcl code calls starttime for example many times per appt.
  270      * cache the last result to avoid expensive timezone conversions
  271      */
  272     mutable struct {
  273         int from_min, to_min;
  274         Date from_d, to_d;
  275         bool to_tz;
  276         void invalidate() { from_min=-1; }
  277     } cache;
  278 };
  279 
  280 #endif /* _ITEMH */