00001 #include "user.h" 00002 00003 #include <QList> 00004 #include <QString> 00005 00006 #include "task.h" 00007 #include "reservation.h" 00008 #include "usertype.h" 00009 #include "invitation.h" 00010 00011 #include <tools/functions.h> 00012 #include <tools/invaliddataexception.h> 00013 00014 namespace domain 00015 { 00016 00017 struct UserPrivate 00018 { 00019 UserType* type; 00020 QString name; 00021 QList<Invitation*> invitations; 00022 QList<Task*> tasks; 00023 }; 00024 00025 User::User(UserType* type, const QString& name) : d(new UserPrivate()) 00026 { 00027 if (name.isEmpty()) 00028 throw tools::InvalidDataException("Cannot create a user with an empty name"); 00029 00030 d->type = type; 00031 d->name = name; 00032 } 00033 00034 User::~User() 00035 { 00036 deleteDependentData(); 00037 00038 delete d; 00039 } 00040 00041 UserType* User::userType() const 00042 { 00043 return d->type; 00044 } 00045 00046 const QString& User::name() const 00047 { 00048 return d->name; 00049 } 00050 00051 void User::addTask(Task* task) 00052 { 00053 Q_ASSERT(!d->tasks.contains(task)); 00054 00055 d->tasks << task; 00056 emit dataChanged(); 00057 } 00058 00059 void User::removeTask(Task* task) 00060 { 00061 if (d->tasks.contains(task)) 00062 { 00063 d->tasks.removeAll(task); 00064 emit dataChanged(); 00065 } 00066 } 00067 00068 QList<const Task*> User::tasks() const 00069 { 00070 return tools::makeConstList(d->tasks); 00071 } 00072 00073 QList<const Invitation*> User::invitations() const 00074 { 00075 return tools::makeConstList(d->invitations); 00076 } 00077 00078 QList<StorableData*> User::dependentData() const 00079 { 00080 QList<StorableData*> ret; 00081 00082 Q_FOREACH (Task* task, d->tasks) 00083 ret << task; 00084 Q_FOREACH (Invitation* invitation, d->invitations) 00085 ret << invitation; 00086 00087 return ret; 00088 } 00089 00090 bool User::isAdmin() const 00091 { 00092 return false; 00093 } 00094 00095 void User::addInvitation(Invitation* invitation) 00096 { 00097 d->invitations << invitation; 00098 emit dataChanged(); 00099 } 00100 00101 void User::removeInvitation(Invitation* invitation) 00102 { 00103 d->invitations.removeAll(invitation); 00104 emit dataChanged(); 00105 } 00106 00107 AdminUser::AdminUser() : User(new AdminUserType(), "Admin") 00108 { 00109 } 00110 00111 AdminUser::~AdminUser() 00112 { 00113 delete static_cast<AdminUserType*>(userType()); 00114 } 00115 00116 bool AdminUser::isAdmin() const 00117 { 00118 return true; 00119 } 00120 00121 }