Source

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

Full commit
Hector Miuler Ma… f8f9ed6 


Hector Miuler Ma… 3b747d5 

Hector Miuler Ma… cca6cd9 


Hector Miuler Ma… 50979f6 




Hector Miuler Ma… ab99dff 


Hector Miuler Ma… f8f9ed6 









Hector Miuler Ma… 2bf3561 












Hector Miuler Ma… f8f9ed6 
Hector Miuler Ma… 3b747d5 
Hector Miuler Ma… 50979f6 
Hector Miuler Ma… cca6cd9 

Hector Miuler Ma… 50979f6 
Hector Miuler Ma… ab99dff 

Hector Miuler Ma… 50979f6 
Hector Miuler Ma… cca6cd9 

Hector Miuler Ma… 50979f6 
Hector Miuler Ma… f8f9ed6 
Hector Miuler Ma… 3b747d5 
Hector Miuler Ma… cca6cd9 
Hector Miuler Ma… 3b747d5 

Hector Miuler Ma… f8f9ed6 











Hector Miuler Ma… 50979f6 

Hector Miuler Ma… f8f9ed6 
Hector Miuler Ma… ab99dff 











Hector Miuler Ma… 50979f6 






Hector Miuler Ma… ab99dff 

Hector Miuler Ma… 2bf3561 

Hector Miuler Ma… f8f9ed6 

Hector Miuler Ma… cca6cd9 







Hector Miuler Ma… f8f9ed6 
Hector Miuler Ma… 3b747d5 
Hector Miuler Ma… 01dcce9 




Hector Miuler Ma… 50979f6 
Hector Miuler Ma… 01dcce9 
Hector Miuler Ma… 3b747d5 

Hector Miuler Ma… 0de00ed 
Hector Miuler Ma… 3b747d5 


Hector Miuler Ma… f8f9ed6 



Hector Miuler Ma… 01dcce9 
Hector Miuler Ma… f8f9ed6 


Hector Miuler Ma… 50979f6 
Hector Miuler Ma… f8f9ed6 


Hector Miuler Ma… 2bf3561 





Hector Miuler Ma… cca6cd9 
Hector Miuler Ma… fc8dee4 


Hector Miuler Ma… cca6cd9 

Hector Miuler Ma… 2bf3561 
Hector Miuler Ma… cca6cd9 

Hector Miuler Ma… ab99dff 



Hector Miuler Ma… 50979f6 
Hector Miuler Ma… ab99dff 





Hector Miuler Ma… 50979f6 
Hector Miuler Ma… ab99dff 




Hector Miuler Ma… 50979f6 
Hector Miuler Ma… cca6cd9 

Hector Miuler Ma… f8f9ed6 
package com.miuler.mocksmtp.gui

import com.trolltech.qt.gui._
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 org.apache.james.mime4j.message.DefaultMessageBuilder
import java.io.ByteArrayInputStream
import org.apache.james.mime4j.dom.Message
import com.trolltech.qt.gui.QPalette.ColorRole
import com.trolltech.qt.core.Qt
import java.util.Date
import org.apache.james.mime4j.parser.MimeStreamParser


/**
 * Created with IntelliJ IDEA.
 * User: miuler
 * Date: 13/09/12
 * Time: 08:16 PM
 * To change this template use File | Settings | File Templates.
 */
class MainGUI(id: String = null, serverId: String = null) {
  QApplication.initialize(Array())
  new MainWindowSMTP()
  QApplication.exec()
}

/**
 * Created with IntelliJ IDEA.
 * User: miuler
 * Date: 13/09/12
 * Time: 08:16 PM
 * To change this template use File | Settings | File Templates.
 */
class MainWindowSMTP(id: String = null, serverId: String = null) extends QWidget {

  private val log = LoggerFactory.getLogger(classOf[MainGUI])
  private val lcd = new QLCDNumber()
  private var btnInitSMTP: QPushButton = _
  private var btnStopSMTP: QPushButton = _
  private var emails = ListBuffer[Message]()
  private var treeView = new QTreeView
  private var model: QStandardItemModel = _

  private var mockSMTP: MockSMTP = _
  private val connectionTheadNetty = new QConnectionThreadNetty
  private var threadConnectionThreadNetty: QThread = _

  initGUI
  initConnectionThread

  private def initGUI {
    btnInitSMTP = new QPushButton()
    btnInitSMTP.setCheckable(true)
    btnInitSMTP.setText("Iniciar servidor SMTP")
    btnInitSMTP.toggled.connect(this, "startSMTP()")

    btnStopSMTP = new QPushButton()
    btnStopSMTP.setCheckable(true)
    btnStopSMTP.setText("Parar servidor SMTP")
    btnStopSMTP.toggle()
    btnStopSMTP.setDisabled(true)
    btnStopSMTP.toggled.connect(this, "stopSMTP()")

    lcd.setDigitCount(3)
    lcd.setBackgroundRole(ColorRole.Dark)

    // ==============================================================
    model = new QStandardItemModel(0, 3, 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"))
    treeView.setRootIsDecorated(false)
    treeView.setAlternatingRowColors(true)
    treeView.setModel(model)
    treeView.setSortingEnabled(true)
    treeView.sortByColumn(1, Qt.SortOrder.AscendingOrder)
    // =============================================================

    val hlayout = new QHBoxLayout()
    hlayout.addWidget(btnInitSMTP)
    hlayout.addWidget(btnStopSMTP)
    hlayout.addWidget(lcd)

    val vlayout = new QVBoxLayout()
    vlayout.addItem(hlayout)
    vlayout.addWidget(treeView)
    setLayout(vlayout)
    setWindowTitle("MOCK SMTP")
    show()
  }

  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() {
    try {
      if (btnInitSMTP.isChecked()) {
        mockSMTP.start
        btnInitSMTP.setDisabled(true)
        btnStopSMTP.toggle()
        btnStopSMTP.setDisabled(false)
        lcd.setBackgroundRole(ColorRole.Light)
      }
    } catch {
      case e: Exception => {
        log.error("Error al iniciar servidor SMTP", e)
        showMessageE(e)
      }
    }
  }

  def stopSMTP() {
    if (btnStopSMTP.isChecked()) {
      mockSMTP.stop
      btnInitSMTP.setDisabled(false)
      btnInitSMTP.toggle()
      btnStopSMTP.setDisabled(true)
      lcd.setBackgroundRole(ColorRole.Dark)
    }
  }

  override def closeEvent(evn: QCloseEvent) {
    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)
    }
  }

  def addEmail(msg: String) {
//    val mimeStream = new MimeStreamParser
//    mimeStream.parse(new ByteArrayInputStream(msg.getBytes()))
    //new Message(new ByteArrayInputStream(msg.getBytes()))

    var email = new DefaultMessageBuilder().parseMessage(new ByteArrayInputStream(msg.getBytes()))
    log.debug("email: {}", email)
    log.debug("email.getMimeType: {}", email.getMimeType)
    log.debug("email.getSubject: {}", email.getSubject())
    log.debug("email.getBody: {}", email.getBody)
    log.debug("email.getBody.dispose: {}", email.getBody().dispose())
    //log.debug("email.getBody: {}", email.getBody().)
    emails.append(email)
    model.insertRow(0)
    model.setData(model.index(0, 0), model.rowCount())
    model.setData(model.index(0, 1), new Date toString)
    model.setData(model.index(0, 2), email.getSubject())
    model.setData(model.index(0, 3), email)
    lcd.display(emails.size)
  }

}