#!/usr/bin/env python
from qt import *
from mainWindowBA import mainWindowBA
from sf import SF
from sfcomm import SFComm
from login import Login
import os
from constants import *
import logging
from ProjectVO import ProjectVO
from PackageVO import PackageVO
from messageQueue import mainMessageQueue
import releaseWizard
#from releaseWizard import ReleaseWizard
from workerThread import WorkerThread
from help import Help
from aboutDialog import AboutDialog
debug = logging.getLogger("mainwindow").debug
error = logging.getLogger("mainwindow").error
COL_PROJ_NAME = 0
COL_PROJ_UNIX = 1
COL_PROJ_GROUP = 2
COL_PKG_NAME = 0
COL_PKG_ID = 1
COL_PKG_STATUS = 2
COL_PKG_FULLACCESS = 3
class mainWindow(mainWindowBA):
def __init__(self, settings, *args):
apply(mainWindowBA.__init__,(self,) + args)
self.projects_dict = {} # maps group_id to set of projectVO's
self.projects = None
self.settings = settings
self.timer = QTimer(self)
self.connect(self.timer, SIGNAL("timeout()"),
self.getStatusMsg)
self.timer.start(500, False)
self.sf_comm = SFComm(settings['data_path'])
self.sf = SF(settings['data_path'], self.sf_comm)
self.connect(self, PYSIGNAL('projects_fetched()'), self.populateProjects)
self.current_project_item = None
self.current_projectVO = None
self.current_package_item = None
self.current_packageVO = None
self.logged_in = False
self.releaseWizards = []
self.threads = []
self.eventDict = {EVENT_REFRESH_PACKAGES: self.packageRefreshEvent,
EVENT_ADD_PACKAGE: self.projectNewPackageEvent}
## self.projectsListView.hideColumn(COL_PROJ_UNIX)
## self.projectsListView.hideColumn(COL_PROJ_GROUP)
## self.packagesListView.hideColumn(COL_PKG_ID)
## self.packagesListView.hideColumn(COL_PKG_STATUS)
## self.packagesListView.hideColumn(COL_PKG_FULLACCESS)
self.show()
self.loginPrompt()
def __del__(self):
for t in self.threads:
running = t.running()
t.stop()
if not t.finished():
t.wait()
def customEvent(self, event):
data = event.data()
etype = event.type()
event_action = self.eventDict.get(etype)
if event_action: event_action(data)
def closeEvent(self, ev):
self.sf_comm.remove_cookie()
ev.accept()
def loginPrompt(self):
if not self.logged_in:
self.login = Login(self, self.settings['data_path'], self.sf)
def getStatusMsg(self):
# read the message queue. If an item exists,
# populate the status bar with it.
msg = mainMessageQueue.get()
if msg:
self.statusBar().message(msg)
def populateProjects(self, projects):
self.projects = projects
self.logged_in = True
self.fileSourceforge_LoginAction.setEnabled(False)
self.projectsListView.clear()
self.current_project_item = None
self.current_projectVO = None
self.current_package_item = None
self.current_packageVO = None
if projects:
for project in projects:
proj_item = QListViewItem(self.projectsListView)
proj_item.setText(COL_PROJ_NAME, project.getProjectName())
proj_item.setText(COL_PROJ_UNIX, project.getUnixName())
proj_item.setText(COL_PROJ_GROUP, project.getGroupId())
self.projects_dict[project.getGroupId()] = project.getPackages()
if len(projects) == 1:
self.projectSelected(proj_item)
def populatePackages(self, group_id):
self.packagesListView.clear()
packages = self.projects_dict.get(group_id)
debug("packages: %s", packages)
self.__initialize_package_fields()
if packages:
for package in packages:
pkg_item = QListViewItem(self.packagesListView)
pkg_item.setText(COL_PKG_NAME, package.getPackageName())
pkg_item.setText(COL_PKG_ID, package.getPackageId())
pkg_item.setText(COL_PKG_STATUS, package.getPackageStatus())
pkg_item.setText(COL_PKG_FULLACCESS, package.getFullAccess())
if len(packages) == 1:
self.packagesListView.setSelected(pkg_item, True)
#self.packageSelected(pkg_item)
def __initialize_package_fields(self):
self.current_package_item = None
self.current_packageVO = None
self.packageIdLineEdit.setText("")
self.updatePackageLineEdit.setText("")
self.newPackageLineEdit.setText("")
# enable
self.refreshPackagesPushButton.setEnabled(1)
self.newPackageLineEdit.setEnabled(1)
# disable
self.AddPackagePushButton.setEnabled(0)
self.newReleasePushButton.setEnabled(0)
self.packageStatusComboBox.setEnabled(0)
self.updatePackagePushButton.setEnabled(0)
self.updatePackageLineEdit.setEnabled(0)
self.newReleasePushButton.setEnabled(0)
def projectSelected(self, item):
self.current_project_item = item
self.current_projectVO = self.__getProjectFromItem(item)
group_id = self.current_projectVO.getGroupId()
debug("projectSelected: %s", group_id)
self.populatePackages(group_id)
def newPackageEdited(self, val):
if not val:
self.AddPackagePushButton.setEnabled(0)
else:
self.AddPackagePushButton.setEnabled(1)
def updatePackageEdited(self, val):
if val:
self.updatePackagePushButton.setEnabled(1)
else:
self.updatePackagePushButton.setEnabled(0)
def updatePackage(self):
package_name = str(self.updatePackageLineEdit.text())
package_status = str(self.packageStatusComboBox.currentText())
package_id = str(self.packageIdLineEdit.text())
if self.sf_comm.update_package(self.current_projectVO.getGroupId(),
package_id,
package_name,
package_status):
self.statusBar().message("Package updated successfully.")
self.packageRefresh()
##self.packageRefresh(PackageVO(package_name,
## package_id,
## package_status))
else:
self.statusBar().message("Could not update package")
def __getPackageFromItem(self, item):
return PackageVO(str(item.text(COL_PKG_NAME)),
str(item.text(COL_PKG_ID)),
str(item.text(COL_PKG_STATUS)),
str(item.text(COL_PKG_FULLACCESS)))
def __getProjectFromItem(self, item):
return ProjectVO(str(item.text(COL_PROJ_NAME)),
str(item.text(COL_PROJ_UNIX)),
str(item.text(COL_PROJ_GROUP)))
def packageSelected(self, item):
self.current_package_item = item
pkg = self.__getPackageFromItem(item)
self.current_packageVO = pkg
self.updatePackageLineEdit.setText(pkg.getPackageName())
status = pkg.getPackageStatus()
if status == 'Active':
self.packageStatusComboBox.setCurrentItem(0)
else:
self.packageStatusComboBox.setCurrentItem(1)
if pkg.getFullAccess() == "True":
self.updatePackageLineEdit.setReadOnly(False)
else:
self.updatePackageLineEdit.setReadOnly(True)
self.packageIdLineEdit.setText(pkg.getPackageId())
#enable
self.packageStatusComboBox.setEnabled(1)
self.updatePackageLineEdit.setEnabled(1)
self.newReleasePushButton.setEnabled(1)
# disable
self.updatePackagePushButton.setEnabled(0)
def packageRefresh(self):
project = self.current_projectVO
group_id = project.getGroupId()
# disable
self.projectsListView.setEnabled(0)
self.setCursor(Qt.waitCursor)
thread = WorkerThread(self,
EVENT_REFRESH_PACKAGES,
self.sf_comm.get_packages,
group_id)
thread.start()
self.threads.append(thread)
def packageRefreshEvent(self, pkgs):
selectedVO = self.current_packageVO
project = self.current_projectVO
group_id = project.getGroupId()
current_pkgs = self.projects_dict[group_id]
if current_pkgs != pkgs:
debug("Packages updated. Adding %d packages", len(pkgs))
# remove the current project from
# the set: self.projects
self.projects.remove(project)
project.setPackages(pkgs)
# re-add this project, with the
# new pkgs to self.projects
self.projects.add(project)
# save them for future runs
self.sf.save_projects()
# update the hash & listview
self.projects_dict[group_id] = pkgs
self.populatePackages(group_id)
else:
debug("No new packages detected")
if selectedVO:
self.__set_package_selected(selectedVO)
# disable
self.updatePackagePushButton.setEnabled(0)
# enable
self.projectsListView.setEnabled(1)
self.setCursor(Qt.arrowCursor)
def __set_package_selected(self, selectedVO):
pkg = self.packagesListView.firstChild()
while pkg != None:
pkgVO = self.__getPackageFromItem(pkg)
if pkgVO.getPackageId() == selectedVO.getPackageId():
self.packagesListView.setCurrentItem(pkg)
break
pkg = pkg.nextSibling()
def projectNewPackage(self):
name = str(self.newPackageLineEdit.text())
if not name:
QMessageBox.information(self, "Package Name",
"Cannot create package..\n"
"Package name is empty.")
return
debug("Creating package: %s", name)
group_id = str(self.projectsListView.currentItem().text(2))
self.setCursor(Qt.waitCursor)
thread = WorkerThread(self,
EVENT_ADD_PACKAGE,
self.sf_comm.add_package,
group_id,
name)
thread.start()
self.threads.append(thread)
def projectNewPackageEvent(self, result):
self.setCursor(Qt.arrowCursor)
debug("add package result: %d", result)
if result:
self.packageRefresh()
def createNewRelease(self):
debug("%s", self.current_packageVO)
release_wizard = releaseWizard.ReleaseWizard(self,
self.settings,
self.sf_comm,
self.current_projectVO.copy(),
self.current_packageVO.copy())
release_wizard.show()
self.releaseWizards.append(release_wizard)
def packageNewRelease(self):
item = self.projectsListView.currentItem()
package = PackageVO(str(item.text(0)), str(item.text(1)))
debug("packageNewRelease(): %s", package)
def help(self):
self._help = Help(self, "mainwindow.html")
def reloadWizard(self):
reload(releaseWizard)
def editCopy(self, *args):
print "mainWindow.editCopy(): Not implemented yet"
def editCut(self, *args):
print "mainWindow.editCut(): Not implemented yet"
def editFind(self, *args):
print "mainWindow.editFind(): Not implemented yet"
def editPaste(self, *args):
print "mainWindow.editPaste(): Not implemented yet"
def editRedo(self, *args):
print "mainWindow.editRedo(): Not implemented yet"
def editUndo(self, *args):
print "mainWindow.editUndo(): Not implemented yet"
def fileExit(self):
self.close()
def fileNew(self, *args):
print "mainWindow.fileNew(): Not implemented yet"
def fileOpen(self, *args):
print "mainWindow.fileOpen(): Not implemented yet"
def filePrint(self, *args):
print "mainWindow.filePrint(): Not implemented yet"
def fileSave(self, *args):
print "mainWindow.fileSave(): Not implemented yet"
def fileSaveAs(self, *args):
print "mainWindow.fileSaveAs(): Not implemented yet"
def helpAbout(self, *args):
self.aboutWindow = AboutDialog(self)
self.aboutWindow.show()
|