user class
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Fri, 29 Feb 2008 09:29:01 +0000 (09:29 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Fri, 29 Feb 2008 09:29:01 +0000 (09:29 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@90 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

src/user.cpp [new file with mode: 0644]
src/user.h [new file with mode: 0644]

diff --git a/src/user.cpp b/src/user.cpp
new file mode 100644 (file)
index 0000000..549de5c
--- /dev/null
@@ -0,0 +1,103 @@
+//
+// C++ Implementation: user
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+
+#include "user.h"
+#include "webrequest.h"
+
+#include <QRegExp>
+#include <QDomElement>
+
+MUser::MUser(MWebRequest*r,QDomElement&e)
+{
+       req=r;
+       m_userid=e.attribute("name").trimmed();
+       m_descr=e.text().trimmed();
+}
+
+MUser::MUser(MWebRequest*r,QString n)
+{
+       req=r;
+       m_userid=n;
+}
+
+MUser::MUser()
+{
+       req=0;
+}
+
+MUser::MUser(const MUser&u)
+{
+       req=u.req;
+       m_userid=u.m_userid;
+       m_descr=u.m_descr;
+}
+
+QString MUser::userId()
+{
+       return m_userid;
+}
+
+QString MUser::description()
+{
+       return m_descr;
+}
+
+bool MUser::isValid()
+{
+       return QRegExp("[A-Za-z0-9_]+").exactMatch(m_userid);
+}
+
+bool MUser::create()
+{
+       //do not attempt to save invalid or incomplete data
+       if(!isValid())return false;
+       //create XML
+       QDomDocument doc;
+       QDomElement root=doc.createElement("Users");
+       QDomElement el=doc.createElement("User");
+       el.setAttribute("name",m_userid);
+       el.appendChild(doc.createTextNode(m_descr));
+       root.appendChild(el);
+       doc.appendChild(root);
+       //call
+       req->request("adduser",doc.toByteArray());
+       //check success
+       if(req->responseStatus()==MWebRequest::Ok){
+               //TODO: check response content for myself, return false if not found
+               return true;
+       }else{
+               return false;
+       }
+}
+
+bool MUser::setDescription(QString d)
+{
+       m_descr=d;
+       //do not attempt to save invalid or incomplete data
+       if(!isValid())return false;
+       //create XML
+       QDomDocument doc;
+       QDomElement root=doc.createElement("Users");
+       QDomElement el=doc.createElement("User");
+       el.setAttribute("name",m_userid);
+       el.appendChild(doc.createTextNode(m_descr));
+       root.appendChild(el);
+       doc.appendChild(root);
+       //call
+       req->request("setuserdescription",doc.toByteArray());
+       //check success
+       if(req->responseStatus()==MWebRequest::Ok){
+               return true;
+       }else{
+               return false;
+       }
+}
diff --git a/src/user.h b/src/user.h
new file mode 100644 (file)
index 0000000..71eed7f
--- /dev/null
@@ -0,0 +1,53 @@
+//
+// C++ Interface: user
+//
+// Description: 
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2007
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+
+#ifndef MAGICSMOKE_USER_H
+#define MAGICSMOKE_USER_H
+
+#include <QString>
+
+class MWebRequest;
+class QDomElement;
+
+class MUser
+{
+       public:
+               /**create invalid user*/
+               MUser();
+               /**create user by name*/
+               MUser(MWebRequest*,QString);
+               /**create user from XML element*/
+               MUser(MWebRequest*,QDomElement&);
+               /**copy user*/
+               MUser(const MUser&);
+               
+               /**returns user name*/
+               QString userId();
+               /**returns user description*/
+               QString description();
+               
+               /**checks user name*/
+               bool isValid();
+               
+               /**creates user in database; returns true on success*/
+               bool create();
+               
+               /**sets new description, both locally and on server*/
+               bool setDescription(QString);
+               
+       private:
+               MWebRequest*req;
+               QString m_userid,m_descr;
+};
+
+
+#endif