Minor: Fixed return value
[biaweb_qt.git] / main_window.py
index b2547b6..799fdf8 100644 (file)
@@ -2,12 +2,15 @@
 # Main Window class
 
 import PyQt4
+import sys
+
 import ui_main_window
 import site_configuration_dialog as scd
 import category_dialog as catd
 import article_dialog as artd
+import templates_dialog as tpld
+import generate_dialog as gend
 import biaweb_db
-import sys
 
 class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        def __init__ (self):
@@ -18,26 +21,35 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        # refresh the category list
        def repopulate_categories (self):
                recs = biaweb_db.get_categories (self.current_db)
+
+               # check with False because None is returned when no records exist
                if recs == False:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the categories")
-                       return
+                       return False
 
                self.categories.clear ()
                for (id, name, desc, stub) in recs:
                        qrow = PyQt4.QtGui.QTreeWidgetItem ([str(id), str(name)])
                        self.categories.addTopLevelItem (qrow)
 
+               # return true when successful
+               return True
+
        # refresh the articles list
        def repopulate_articles (self, catid=None):
                recs = biaweb_db.get_articles (self.current_db, catid)
+
+               # check with False because None is returned when no records exist
                if recs == False:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in getting the articles")
-                       return
+                       return False
 
                self.articles.clear ()
                for item in recs:
                        qrow = PyQt4.QtGui.QTreeWidgetItem ([str(item[0]), str(item[1])])
                        self.articles.addTopLevelItem (qrow)
+               # return true when successful
+               return True
 
        # when the view is refreshed
        def onViewRefresh (self):
@@ -47,7 +59,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
 
        # when add article is triggered
        def onArticleAdd (self):
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                        "Cannot create article. You need to create or open a website first")
                else:
@@ -57,16 +69,114 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                PyQt4.QtGui.QMessageBox.critical (self, "Error", "No category selected for article")
                                return
                        artdlg = artd.ArticleDialog (self)
-                       artdlg.exec_ ()
+                       cats = biaweb_db.get_categories (self.current_db)
 
+                       artdlg.populate_categories (cats, catid)
+
+                       # if OK is pressed
+                       if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
+                               title = str (artdlg.article_title.text ()).strip ()
+                               keywords = str (artdlg.keywords.text ()).strip ()
+                               summary = str (artdlg.summary.toPlainText ()).strip ()
+                               content = str (artdlg.content.toPlainText ()).strip ()
+                               cid, ok = artdlg.category.itemData (artdlg.category.currentIndex ()).toInt ()
+                               # if catid is not an integer then abort
+                               if not ok:
+                                       return
+                               rating = artdlg.rating.value ()
+                               stub = str (artdlg.stub.text ()).strip ()
+
+                               ret = biaweb_db.create_article (self.current_db, title, summary, keywords, content, cid,
+                                                                                               stub, rating)
+                               if ret:
+                                       PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully created")
+                                       self.repopulate_categories ()
+                                       self.repopulate_articles (catid)
+                               else:
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in creating article")
 
        # when edit article is triggered
        def onArticleEdit (self):
-               pass
+               if self.current_db is None:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                                       "Cannot edit article. You need to create or open a website first")
+               else:
+                       # get the selected article
+                       artid = self.get_selected_item_id (self.articles)
+                       catid = self.get_selected_item_id (self.categories)
+
+                       # no article is selected
+                       if artid is None:
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
+                               return
+                       article = biaweb_db.get_article (self.current_db, artid)
+                       # if article cannot be read
+                       if not article:
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading article")
+                               return
+                       artdlg = artd.ArticleDialog (self)
+
+                       artdlg.article_title.setText (article[1])
+                       artdlg.summary.setPlainText (article[2])
+                       artdlg.keywords.setText (article[3])
+                       artdlg.content.setPlainText (article[4])
+                       # populate category combo box and set active category to the article's category
+                       cats = biaweb_db.get_categories (self.current_db)
+                       artdlg.populate_categories (cats, article[7])
+                       artdlg.stub.setText (article[8])
+                       artdlg.rating.setValue (article[9])
+
+                       # ok is pressed
+                       if artdlg.exec_ () == PyQt4.QtGui.QDialog.Accepted:
+                               title = str (artdlg.article_title.text ()).strip ()
+                               summary = str (artdlg.summary.toPlainText ()).strip ()
+                               keywords = str (artdlg.keywords.text ()).strip ()
+                               content = str (artdlg.content.toPlainText ()).strip ()
+                               cid, ok = artdlg.category.itemData (artdlg.category.currentIndex()).toInt ()
+                               # if cat id is not an integer
+                               if not ok:
+                                       return
+                               rating = artdlg.rating.value ()
+                               stub = str (artdlg.stub.text ()).strip ()
+                               ret = biaweb_db.update_article (self.current_db, artid, title,
+                                                                               summary, keywords, content, cid, stub, rating)
+                               if ret:
+                                       PyQt4.QtGui.QMessageBox.information (self, "Success", "Article successfully updated")
+                                       self.repopulate_articles (catid)
+                               else:
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating article")
 
        # when delete article is triggered
        def onArticleDelete (self):
-               pass
+               if self.current_db is None:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                                       "Cannot delete article. You need to create or open a website first")
+               else:
+                       # get the selected article
+                       artid = self.get_selected_item_id (self.articles)
+                       catid = self.get_selected_item_id (self.categories)
+                       if artid is None:
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error", "No article selected")
+                               return
+                       # get confirmation on delete
+                       flag = PyQt4.QtGui.QMessageBox.question (self, "Confirm",
+                                                       "Are you sure you wish to delete the selected article?",
+                                                       PyQt4.QtGui.QMessageBox.Yes, PyQt4.QtGui.QMessageBox.No)
+                       # confirmed
+                       if flag == PyQt4.QtGui.QMessageBox.Yes:
+                               ret = biaweb_db.delete_article (self.current_db, artid)
+                               if not ret:
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting article")
+                               else:
+                                       self.repopulate_articles (catid)
+
+       # when category item is activated
+       def onCategoryItemActivated (self):
+               self.onCategoryEdit ()
+
+       # when article item is activated
+       def onArticleItemActivated (self):
+               self.onArticleEdit ()
 
        # when item selection is changed in categories tree widget
        def onCategorySelectionChanged (self):
@@ -74,16 +184,16 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                        catid = self.get_selected_item_id (self.categories)
                        self.repopulate_articles (catid)
 
-       # when configuration menu is activated
+       # when configuration menu is triggered
        def onConfiguration (self):
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                        "Cannot edit configuration. You need to create or open a website first")
                else:
                        dlg = scd.SiteConfigDialog (self)
                        configs = biaweb_db.get_configuration (self.current_db)
 
-                       if configs == False:
+                       if not configs:
                                PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading configuration")
                                return
 
@@ -108,10 +218,19 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                # database update
                                flag = biaweb_db.set_configuration (self.current_db, site_title, site_url, keywords, description,
                                                                                        copyright, num_rss, destination)
-                               if flag == False:
+                               if not flag:
                                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                                                        "SQLite 3 error in updating configuration")
 
+       # when templates menu is triggered
+       def onTemplates (self):
+               if self.current_db is None:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                                               "Cannot edit templates. You need to create or open a website first")
+               else:
+                       tdlg = tpld.TemplatesDialog (self, self.current_db)
+                       tdlg.exec_ ()
+
        # function to get the category or article ID from current selected item in a tree widget
        def get_selected_item_id (self, twidget):
                selitems = twidget.selectedItems ()
@@ -125,7 +244,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        # category edit action
        def onCategoryEdit (self):
                # if no database is open
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                        "Cannot edit category. You need to create or open a website first")
                # database is open
@@ -138,8 +257,8 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                        else:
                                cat = biaweb_db.get_category (self.current_db, catid)
                                # if the category cannot be retrieved from database
-                               if cat == False:
-                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error reading category")
+                               if not cat:
+                                       PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in reading category")
                                        return
 
                                # set the data in the dialog
@@ -156,8 +275,9 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
 
                                        ret = biaweb_db.update_category (self.current_db,
                                                                                                catid, category_name, category_desc, category_stub)
-                                       if ret == True:
+                                       if ret:
                                                PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully updated")
+                                               self.repopulate_categories ()
                                        else:
                                                PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in updating category")
 
@@ -165,9 +285,9 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        # category delete action
        def onCategoryDelete (self):
                # if there is no database
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
-                                                               "Cannot edit category. You need to create or open a website first")
+                                                               "Cannot delete category. You need to create or open a website first")
                else:
                        # get the selected category
                        catid = self.get_selected_item_id (self.categories)
@@ -182,7 +302,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                # if confirmed
                                if flag == PyQt4.QtGui.QMessageBox.Yes:
                                        ret = biaweb_db.remove_category (self.current_db, catid)
-                                       if ret == False:
+                                       if not ret:
                                                PyQt4.QtGui.QMessageBox.critical (self, "Error", "SQLite 3 error in deleting category")
                                        else:
                                                self.repopulate_categories ()
@@ -191,7 +311,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
        # category add action
        def onCategoryAdd (self):
                # if there is no database
-               if self.current_db == None:
+               if self.current_db is None:
                        PyQt4.QtGui.QMessageBox.critical (self, "Error",
                                                        "Cannot add category. You need to create or open a website first")
                else:
@@ -203,7 +323,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                cat_desc = str (dlg.category_desc.text ()).strip ()
                                cat_stub = str (dlg.category_stub.text ()).strip ()
                                ret = biaweb_db.create_category (self.current_db, cat_name, cat_desc, cat_stub)
-                               if ret == True:
+                               if ret:
                                        PyQt4.QtGui.QMessageBox.information (self, "Success", "Category successfully created")
                                        self.repopulate_categories ()
                                        self.repopulate_articles ()
@@ -215,9 +335,21 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                filename = PyQt4.QtGui.QFileDialog.getOpenFileName (self, "Open Site Database")
                if filename:
                        self.current_db = str (filename)
-                       self.setWindowTitle ("BiaWeb - " + self.current_db)
-                       self.repopulate_categories ()
-                       self.repopulate_articles ()
+
+                       # added to check whether categories are loaded successfully or not
+                       # if not, then reset the current_db to None
+                       loaded_cats = self.repopulate_categories ()
+                       loaded_arts = self.repopulate_articles ()
+                       # if failed in loading either categories or articles (note: checking against
+                       # False and not None)
+                       if loaded_cats == False or loaded_arts == False:
+                               self.current_db = None
+                               PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                       "SQLite 3 error in loading site database. This appears to be an invalid BiaWeb database")
+                       else:
+                               # set the window title to the database
+                               self.setWindowTitle ("BiaWeb - " + self.current_db)
+
 
        # file new menu is clicked
        def onFileNew (self):
@@ -240,7 +372,7 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                self.setWindowTitle ("BiaWeb - " + self.current_db)
                                flag = biaweb_db.create_db (self.current_db, site_title, site_url, keywords, description,
                                                                copyright, num_rss, destination)
-                               if flag == True:
+                               if flag:
                                        PyQt4.QtGui.QMessageBox.information (self, "Success",
                                                                                                                        "New site db successfully created")
                                        self.articles.clear ()
@@ -248,6 +380,23 @@ class MainWindow (PyQt4.QtGui.QMainWindow, ui_main_window.Ui_MainWindow):
                                else:
                                        PyQt4.QtGui.QMessageBox.critical (self, "Error", "System or SQLite 3 error in creating database")
 
+       # when generate site menu is triggered
+       def onGenerateSite (self):
+               if self.current_db is None:
+                       PyQt4.QtGui.QMessageBox.critical (self, "Error",
+                                                               "Cannot generate site. You need to create or open a website first")
+               else:
+                       # run the generate site dialog
+                       gendlg = gend.GenerateDialog (self, self.current_db)
+                       gendlg.exec_ ()
+
+       # about menu is triggered
+       def onAbout (self):
+               PyQt4.QtGui.QMessageBox.about (self, "BiaWeb Qt",
+                       "<b>A static website/weblog content management system</b><br /><br />\
+Copyright &copy; 2010 <a href=\"http://www.harishankar.org\">Harishankar</a><br />\
+Licensed under GNU/GPL v3")
+
        # file quit is clicked
        def onFileQuit (self):
                sys.exit (0)