Source

miuler-mocksmtp / src / main / scala / com / miuler / mocksmtp / gui / MainGUI.scala

Diff from to

src/main/scala/com/miuler/mocksmtp/gui/MainGUI.scala

 package com.miuler.mocksmtp.gui
 
-import com.trolltech.qt.gui._
+import java.util.Properties
+import java.io.{ByteArrayOutputStream, InputStreamReader, BufferedReader, ByteArrayInputStream}
+import javax.mail.Session
+import javax.mail.internet.MimeMessage
+import collection.mutable.ListBuffer
+import org.joda.time.DateTime
+import org.slf4j.LoggerFactory
 import com.miuler.mocksmtp.core.MockSMTP
-import org.slf4j.LoggerFactory
 import com.miuler.mocksmtp.utils.Utils.showMessageE
 import com.trolltech.qt.QThread
 import com.trolltech.qt.core.Qt.ConnectionType
-import collection.mutable.ListBuffer
-import java.io.ByteArrayInputStream
+import com.trolltech.qt.gui.QSystemTrayIcon.ActivationReason
+//     com.trolltech.qt.gui.QSystemTrayIcon$ActivationReason
+//                          QSystemTrayIcon::ActivationReason
 import com.trolltech.qt.gui.QPalette.ColorRole
-import com.trolltech.qt.core.Qt
-import javax.mail.Session
-import javax.mail.internet.MimeMessage
-import java.util.Properties
-import org.joda.time.DateTime
+import com.trolltech.qt.core.{QModelIndex, Qt}
+import com.trolltech.qt.gui._
 
 /**
  * Created with IntelliJ IDEA.
 
   private val log = LoggerFactory.getLogger(classOf[MainGUI])
   private val session = Session.getInstance(new Properties)
-  private var emails = ListBuffer[MimeMessage]()
+  private val emails = ListBuffer[MimeMessage]()
   private val lcd = new QLCDNumber()
-  private var btnInitSMTP: QPushButton = _
-  private var btnStopSMTP: QPushButton = _
-  private var treeView = new QTreeView
+  private val btnInitSMTP: QPushButton = new QPushButton()
+  private val btnStopSMTP: QPushButton = new QPushButton()
+  private val treeView = new QTreeView
   private var model: QStandardItemModel = _
+  private val systray = new QSystemTrayIcon(this)
 
   private var mockSMTP: MockSMTP = _
   private val connectionTheadNetty = new QConnectionThreadNetty
-  private var threadConnectionThreadNetty: QThread = _
+  private var threadConnectionThreadNetty = new QThread (connectionTheadNetty)
 
   initGUI
   initConnectionThread
 
   private def initGUI {
     // =========================================================================
-    btnInitSMTP = new QPushButton()
     btnInitSMTP.setCheckable(true)
     btnInitSMTP.setText("Iniciar servidor SMTP")
-    btnInitSMTP.toggled.connect(this, "startSMTP()")
+    btnInitSMTP.toggled.connect(this, "onStartSMTP()")
 
-    btnStopSMTP = new QPushButton()
     btnStopSMTP.setCheckable(true)
     btnStopSMTP.setText("Parar servidor SMTP")
     btnStopSMTP.toggle()
     btnStopSMTP.setDisabled(true)
-    btnStopSMTP.toggled.connect(this, "stopSMTP()")
+    btnStopSMTP.toggled.connect(this, "onStopSMTP()")
 
     val btnClean = new QPushButton("Borrar correos")
-    btnClean.clicked.connect(this, "cleanEmails()")
+    btnClean.clicked.connect(this, "onCleanEmails()")
     // =========================================================================
 
     // =========================================================================
     // =========================================================================
 
     // =========================================================================
-    model = new QStandardItemModel(0, 3, this)
+    systray.setToolTip("Servidor mock de correos")
+    systray.setIcon(new QIcon(new QPixmap("classpath:smtp_server_16x16_off.png")))
+    systray.activated.connect(this, "onActivated(com.trolltech.qt.gui.QSystemTrayIcon$ActivationReason)");
+    systray.show()
+    // =========================================================================
+
+    // =========================================================================
+    model = new QStandardItemModel(0, 5, this)
     model.setHeaderData(0, Qt.Orientation.Horizontal, tr("id"))
     model.setHeaderData(1, Qt.Orientation.Horizontal, tr("Fecha de envio"))
     model.setHeaderData(2, Qt.Orientation.Horizontal, tr("Asunto"))
+
+    var standardItem = new QStandardItem()
+    standardItem.setEditable(false)
+    model.setItemPrototype(standardItem)
     treeView.setRootIsDecorated(false)
     treeView.setAlternatingRowColors(true)
     treeView.setModel(model)
     treeView.setSortingEnabled(true)
     treeView.sortByColumn(0, Qt.SortOrder.AscendingOrder)
     treeView.setMinimumSize(300, 200) //TODO: @Miuler: Esto es para evitar un error en algunas maquinas
+    treeView.resize(300, 200)
+    treeView.doubleClicked.connect(this, "onViewEmail(com.trolltech.qt.core.QModelIndex)")
+    treeView.hideColumn(3)
+    treeView.hideColumn(4)
     // =========================================================================
 
     // =========================================================================
   }
 
   def initConnectionThread {
-    threadConnectionThreadNetty = new QThread (connectionTheadNetty)
     connectionTheadNetty.moveToThread(threadConnectionThreadNetty)
     connectionTheadNetty.messageSignal.connect(this, "addEmail(String)", ConnectionType.QueuedConnection)
     threadConnectionThreadNetty.start
     mockSMTP = new MockSMTP(connectionTheadNetty)
   }
 
-  def startSMTP() {
+  def onStartSMTP() {
     try {
       if (btnInitSMTP.isChecked()) {
         mockSMTP.start
         btnStopSMTP.toggle()
         btnStopSMTP.setDisabled(false)
         lcd.setBackgroundRole(ColorRole.Light)
+        systray.setIcon(new QIcon(new QPixmap("classpath:smtp_server_16x16.png")))
       }
     } catch {
       case e: Exception => {
     }
   }
 
-  def stopSMTP() {
+  def onStopSMTP() {
     if (btnStopSMTP.isChecked()) {
       mockSMTP.stop
       btnInitSMTP.setDisabled(false)
       btnInitSMTP.toggle()
       btnStopSMTP.setDisabled(true)
       lcd.setBackgroundRole(ColorRole.Dark)
+      systray.setIcon(new QIcon(new QPixmap("classpath:smtp_server_16x16_off.png")))
     }
   }
 
   override def closeEvent(evn: QCloseEvent) {
+    log.debug("systray.isVisible: {}", systray.isVisible)
     log.debug("salir")
     if (btnInitSMTP.isChecked()) {
       log.debug("Apagando el servidor")
       mockSMTP.stop
     }
     if (threadConnectionThreadNetty.isAlive) {
-      log.debug("threadConnectionThreadNetty isAlive: {}", threadConnectionThreadNetty.isAlive)
       connectionTheadNetty.loopConnectionNetty = false
       connectionTheadNetty.mutex.unlock
-      log.debug("threadConnectionThreadNetty isAlive: {}", threadConnectionThreadNetty.isAlive)
     }
   }
 
     val email = new MimeMessage(session, new ByteArrayInputStream(msg.getBytes()))
     val date = new DateTime()
 
+    systray.showMessage("LLego un correo", email.getSubject)
+
     emails.append(email)
     model.insertRow(0)
     model.setData(model.index(0, 0), model.rowCount())
     lcd.display(model.rowCount())
   }
 
-  def cleanEmails() {
-    emails.clear()
-    model.removeRows(0, model.rowCount())
+  def onCleanEmails() {
+    emails.clear
+    model.removeRows(0, model.rowCount)
     lcd.display(0)
   }
 
+  def onActivated(reason: ActivationReason) {
+    println("reason: ", reason)
+    println("reason.value: ", reason.value)
+    if (reason.value == 3) {
+      if (this.isHidden) this.show() else
+        this.hide()
+    } else if (reason.value == 1) {
+      //mostrar menu
+    }
+  }
+
+  def onViewEmail(modelIndex: QModelIndex) {
+    val email = model.data(modelIndex.row, 4).asInstanceOf[MimeMessage]
+    val out = new ByteArrayOutputStream
+    email.writeTo(out)
+
+    val text = new QTextEdit
+    text.setText(out.toString)
+
+    val vlayout = new QVBoxLayout
+    vlayout.addWidget(text)
+
+    val windowEmail = new QWidget()
+    windowEmail.setWindowTitle("EMAIL: " + email.getSubject)
+    windowEmail.setMinimumSize(650, 500) //TODO: @Miuler: Esto es para evitar un error en algunas maquinas
+    windowEmail.resize(650, 500)
+    windowEmail.setLayout(vlayout)
+    windowEmail.show()
+  }
+
 }