Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
- AccountsComboBoxModel.java Now only updated when the wallet is fully
synced.
- The sound on each transaction now plays only once.
- During synchronization, the list of messages is updated at the end. So
right displays the number of confirmations.
- Some optimizations.
  • Loading branch information
agran committed May 27, 2015
1 parent 0717b7a commit 700eb34
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 76 deletions.
6 changes: 4 additions & 2 deletions Qora/src/gui/models/AccountsComboBoxModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public void update(Observable o, Object arg)
public synchronized void syncUpdate(Observable o, Object arg)
{
ObserverMessage message = (ObserverMessage) arg;

if(message.getType() == ObserverMessage.ADD_BALANCE_TYPE || message.getType() == ObserverMessage.REMOVE_BALANCE_TYPE || message.getType() == ObserverMessage.ADD_ACCOUNT_TYPE || message.getType() == ObserverMessage.REMOVE_ACCOUNT_TYPE)

if((message.getType() == ObserverMessage.NETWORK_STATUS && (int) message.getValue() == Controller.STATUS_OKE)
||((Controller.getInstance().getStatus() == Controller.STATUS_OKE) && (message.getType() == ObserverMessage.ADD_BALANCE_TYPE || message.getType() == ObserverMessage.REMOVE_BALANCE_TYPE || message.getType() == ObserverMessage.ADD_ACCOUNT_TYPE || message.getType() == ObserverMessage.REMOVE_ACCOUNT_TYPE)))
{
//GET SELECTED ITEM
Account selected = (Account) this.getSelectedItem();
Expand All @@ -66,6 +67,7 @@ public synchronized void syncUpdate(Observable o, Object arg)
{
this.setSelectedItem(selected);
}

}
}

Expand Down
87 changes: 45 additions & 42 deletions Qora/src/gui/models/MessagesTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import qora.account.Account;
import qora.account.PrivateKeyAccount;
import qora.block.Block;
import qora.crypto.AEScrypto;
import qora.transaction.MessageTransaction;
import qora.transaction.Transaction;
Expand Down Expand Up @@ -81,23 +82,23 @@ public MessagesTableModel()
topRenderer.setVerticalAlignment(DefaultTableCellRenderer.TOP);
this.getColumn("").setCellRenderer( topRenderer );

List<Pair<Account, Transaction>> transaction = Controller.getInstance().getLastTransactions(10000);
List<Pair<Account, Transaction>> transaction = Controller.getInstance().getLastTransactions(30000);

for (int i = 0; i < transaction.size(); i++) {
if(transaction.get(i).getB().getType() == Transaction.MESSAGE_TRANSACTION)
for (Pair<Account, Transaction> pair : transaction)
{
if(pair.getB().getType() == Transaction.MESSAGE_TRANSACTION)
{
boolean is = false;

for ( int j = messageBufs.size()-1; j >= 0; j-- )
{
if(Arrays.equals(transaction.get(i).getB().getSignature(),messageBufs.get(j).getSignature()))
for (MessageBuf message : messageBufs) {
if(Arrays.equals(pair.getB().getSignature(), message.getSignature()))
{
is = true;
break;
}
}
if(!is)
{
addMessage(messageBufs.size(),(MessageTransaction)transaction.get(i).getB());
addMessage(messageBufs.size(),(MessageTransaction)pair.getB());
}
}
}
Expand Down Expand Up @@ -327,6 +328,7 @@ public synchronized void syncUpdate(Observable o, Object arg)
{

ObserverMessage message = (ObserverMessage) arg;

if(message.getType() == ObserverMessage.WALLET_STATUS)
{
int status = (int) message.getValue();
Expand All @@ -342,19 +344,14 @@ public synchronized void syncUpdate(Observable o, Object arg)
{
if(Controller.getInstance().getStatus() == Controller.STATUS_OKE)
{
new Thread()
{
public void run()
{
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
updateBlock();
}
}.start();
this.repaint();
}
else if (message.getType() == ObserverMessage.ADD_BLOCK_TYPE &&
((Block)message.getValue()).getHeight() == Controller.getInstance().getMaxPeerHeight())
{
this.repaint();
}

}

if(message.getType() == ObserverMessage.ADD_TRANSACTION_TYPE)
Expand All @@ -364,10 +361,11 @@ public void run()
{
is = false;
for ( int i = messageBufs.size()-1; i >= 0; i-- )
{
if(Arrays.equals(((MessageTransaction) message.getValue()).getSignature(), messageBufs.get(i).getSignature()))
for (MessageBuf messageBuf : messageBufs) {
if(Arrays.equals(((MessageTransaction) message.getValue()).getSignature(), messageBuf.getSignature()))
{
is = true;
break;
}
}
if(!is)
Expand Down Expand Up @@ -405,7 +403,6 @@ private void addMessage(int pos, MessageTransaction transaction)
transaction.getFee(),
transaction.getSignature(),
transaction.getCreator().getPublicKey(),
transaction.getConfirmations(),
transaction.isText()
));
}
Expand Down Expand Up @@ -532,7 +529,7 @@ private void setHeight(int row)
this.setRowHeight(row, textHeight);
}


/*
private void updateBlock()
{
for (int j = 0; j < messageBufs.size(); j++)
Expand All @@ -553,7 +550,7 @@ private void updateBlock()
}
this.repaint();
}

*/
int lineCount( String text )
{
int lineCount = 1;
Expand Down Expand Up @@ -583,9 +580,8 @@ public class MessageBuf
private BigDecimal amount;
private BigDecimal fee;
private byte[] signature;
private int confirmations;

public MessageBuf( byte[] rawMessage, boolean encrypted, String sender, String recipient, long timestamp, BigDecimal amount, BigDecimal fee, byte[] signature, byte[] senderPublicKey, int confirmations, boolean isText )
public MessageBuf( byte[] rawMessage, boolean encrypted, String sender, String recipient, long timestamp, BigDecimal amount, BigDecimal fee, byte[] signature, byte[] senderPublicKey, boolean isText )
{
this.rawMessage = rawMessage;
this.encrypted = encrypted;
Expand All @@ -599,7 +595,6 @@ public MessageBuf( byte[] rawMessage, boolean encrypted, String sender, String r
this.senderPublicKey = senderPublicKey;
this.recipientPublicKey = null;
this.signature = signature;
this.confirmations = confirmations;
this.isText = isText;
}

Expand Down Expand Up @@ -676,12 +671,16 @@ public void setDecryptedMessage(String decryptedMessage)
}
public int getConfirmations()
{
return this.confirmations;
}
public void setConfirmations(int confirmations)
{
this.confirmations = confirmations;
if( DBSet.getInstance().getTransactionMap().contains(this.signature) )
{
return 0;
}
else
{
return Controller.getInstance().getTransaction(this.signature).getConfirmations();
}
}

public boolean isText()
{
return isText;
Expand Down Expand Up @@ -721,11 +720,13 @@ public String getDecrMessageHtml(int width, boolean selected, boolean images)
imglock = "<img src='file:images/messages/unlockedred.png'>";
}

String confirmations = Integer.toString( this.confirmations );
int confirmations = getConfirmations();

String strconfirmations = Integer.toString( confirmations );

if( this.confirmations < 1 )
if( confirmations < 1 )
{
confirmations = "<font color=red>" + confirmations +"</font>";
strconfirmations = "<font color=red>" + strconfirmations +"</font>";
}

String colorHeader = "F0F0F0";
Expand Down Expand Up @@ -764,7 +765,7 @@ public String getDecrMessageHtml(int width, boolean selected, boolean images)
+ "\n<br>\nTo:"
+ recipient+"\n</font></td>\n"
+ "<td bgcolor='" + colorHeader + "' align='right' width='" + (width/2-1) + "'>\n"
+ "<font color='" + colorTextHeader + "'>\n" + confirmations + " . "
+ "<font color='" + colorTextHeader + "'>\n" + strconfirmations + " . "
+ format.format(date) + "\n<br>\n"
+ "Amount: " + amount.toPlainString()+" Fee: "
+ fee.toPlainString()
Expand Down Expand Up @@ -813,20 +814,22 @@ public String getDecrMessageTXT()
{
imglock = "Unencrypted";
}

String confirmations = Integer.toString( this.confirmations );

if( this.confirmations < 1 )
int confirmations = getConfirmations();

String strconfirmations = Integer.toString( confirmations );

if( confirmations < 1 )
{
confirmations = confirmations + " !";
strconfirmations = strconfirmations + " !";
}

return "Date: " + format.format(date) + "\n"
+ "Sender: " + sender + "\n"
+ "Recipient: " + recipient + "\n"
+ "Amount: " + amount.toPlainString() + " Fee: " + fee.toPlainString() + "\n"
+ "Type: " + imginout + ". " + imglock + "\n"
+ "Confirmations: " + confirmations + "\n"
+ "Confirmations: " + strconfirmations + "\n"
+ "[MESSAGE START]\n"
+ getDecrMessage() + "\n"
+ "[MESSAGE END]\n";
Expand Down
13 changes: 7 additions & 6 deletions Qora/src/gui/models/WalletTransactionsTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.mapdb.Fun.Tuple2;

import controller.Controller;
import database.DBSet;
import database.SortableList;
import database.wallet.TransactionMap;
import qora.account.Account;
Expand Down Expand Up @@ -151,7 +152,7 @@ public synchronized void syncUpdate(Observable o, Object arg)

if(Controller.getInstance().getStatus() == Controller.STATUS_OKE && message.getType() == ObserverMessage.ADD_TRANSACTION_TYPE)
{
if(((Transaction) message.getValue()).getParent() == null)
if(DBSet.getInstance().getTransactionMap().contains(((Transaction) message.getValue()).getSignature()))
{
if(((Transaction) message.getValue()).getType() == Transaction.PAYMENT_TRANSACTION)
{
Expand All @@ -160,12 +161,12 @@ public synchronized void syncUpdate(Observable o, Object arg)
{
if(Settings.getInstance().isSoundReceivePaymentEnabled())
{
PlaySound.playSound("receivepayment.wav");
PlaySound.getInstance().playSound("receivepayment.wav", ((Transaction) message.getValue()).getSignature());
}
}
else if(Settings.getInstance().isSoundNewTransactionEnabled())
{
PlaySound.playSound("newtransaction.wav");
PlaySound.getInstance().playSound("newtransaction.wav", ((Transaction) message.getValue()).getSignature());
}
}
else if(((Transaction) message.getValue()).getType() == Transaction.MESSAGE_TRANSACTION)
Expand All @@ -175,17 +176,17 @@ else if(((Transaction) message.getValue()).getType() == Transaction.MESSAGE_TRAN
{
if(Settings.getInstance().isSoundReceiveMessageEnabled())
{
PlaySound.playSound("receivemessage.wav");
PlaySound.getInstance().playSound("receivemessage.wav", ((Transaction) message.getValue()).getSignature()) ;
}
}
else if(Settings.getInstance().isSoundNewTransactionEnabled())
{
PlaySound.playSound("newtransaction.wav");
PlaySound.getInstance().playSound("newtransaction.wav", ((Transaction) message.getValue()).getSignature());
}
}
else if(Settings.getInstance().isSoundNewTransactionEnabled())
{
PlaySound.playSound("newtransaction.wav");
PlaySound.getInstance().playSound("newtransaction.wav", ((Transaction) message.getValue()).getSignature());
}
}
}
Expand Down
86 changes: 60 additions & 26 deletions Qora/src/utils/PlaySound.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
Expand All @@ -10,32 +12,64 @@
import javax.sound.sampled.UnsupportedAudioFileException;

public class PlaySound {
public static void playSound(final String url) {
new Thread(new Runnable() {

public void run() {

try {
File soundFile = new File( "sounds/" + url );

AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);

Clip clip = AudioSystem.getClip();

clip.open(ais);

clip.setFramePosition(0);
clip.start();

Thread.sleep(clip.getMicrosecondLength()/1000);
clip.stop();
clip.close();
} catch(IOException | UnsupportedAudioFileException | LineUnavailableException exc) {
exc.printStackTrace();
} catch (InterruptedException exc) {}

}
}).start();
private static PlaySound instance;
private ArrayList<byte[]> transactionsAlreadyPlayed;

public static PlaySound getInstance()
{
if(instance == null)
{
instance = new PlaySound();
}

return instance;
}

public PlaySound()
{
transactionsAlreadyPlayed = new ArrayList<byte[]>();
}

public void playSound(final String url, byte[] signature) {

boolean is = false;

for (byte[] transactionSign : transactionsAlreadyPlayed) {
if(Arrays.equals(transactionSign, signature))
{
is = true;
break;
}
}

if(!is)
{
transactionsAlreadyPlayed.add(0, signature);

new Thread(new Runnable() {
public void run() {
try {
File soundFile = new File( "sounds/" + url );

AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);

Clip clip = AudioSystem.getClip();

clip.open(ais);

clip.setFramePosition(0);
clip.start();

Thread.sleep(clip.getMicrosecondLength()/1000);
clip.stop();
clip.close();
} catch(IOException | UnsupportedAudioFileException | LineUnavailableException exc) {
exc.printStackTrace();
} catch (InterruptedException exc) {}

}
}).start();
}
}

}

0 comments on commit 700eb34

Please sign in to comment.