Changeset 305


Ignore:
Timestamp:
09/12/08 17:19:37 (16 years ago)
Author:
ra33
Message:

Updated mail stuff and networking stuff!!

Location:
trunk/src/org/expeditee
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Actions.java

    r298 r305  
    257257
    258258        /**
    259          * Temporary, if a plugin system is devised then this would porbably become redundant.
    260          * For now this allows external agents to be included.
    261          *
     259         * Temporary, if a plugin system is devised then this would porbably become
     260         * redundant. For now this allows external agents to be included.
     261         * 
    262262         * @param fullClassNames
    263          *              A set of full class names, that is, the class package and name. For example"
    264          *              "org.myplugin.agents.SerializedSearch"
    265          *
    266          * @return
    267          *              A collection of classes their were omitted because either there was a name clash
    268          *              with existing agents or did not exist.
    269          *              i.e. is completely successful this will be empty. Never null.
     263         *            A set of full class names, that is, the class package and
     264         *            name. For example" "org.myplugin.agents.SerializedSearch"
     265         *
     266         * @return A collection of classes their were omitted because either there
     267         *         was a name clash with existing agents or did not exist. i.e. is
     268         *         completely successful this will be empty. Never null.
    270269         *
    271270         * @throws NullPointerException
    272          *              If fullClassNames is null.
    273          *             
     271         *             If fullClassNames is null.
     272         * 
    274273         */
    275274        public static Collection<String> addAgents(Set<String> fullClassNames) {
    276                 if (fullClassNames == null) throw new NullPointerException("fullClassNames");
    277                
     275                if (fullClassNames == null)
     276                        throw new NullPointerException("fullClassNames");
     277
    278278                List<String> omittedAgents = new LinkedList<String>();
    279                
     279
    280280                for (String fullName : fullClassNames) {
    281                        
    282                         if (fullName == null || fullName.length() == 0) continue;
    283                        
     281
     282                        if (fullName == null || fullName.length() == 0)
     283                                continue;
     284
    284285                        boolean didAdd = false;
    285                
     286
    286287                        try {
    287288                                // Does the class even exist?
    288289                                Class c = Class.forName(fullName);
    289                                
     290
    290291                                String name = c.getSimpleName().toLowerCase();
    291292
    292293                                if (!_JAGs.containsKey(name)) {
    293                                        
     294
    294295                                        _JAGs.put(name, fullName);
    295296                                        didAdd = true;
    296                                        
     297
    297298                                }
    298299
     
    300301                                e.printStackTrace();
    301302                        }
    302                        
    303                         if (!didAdd) omittedAgents.add(fullName);
    304        
    305                 }
    306                        
    307        
     303
     304                        if (!didAdd)
     305                                omittedAgents.add(fullName);
     306
     307                }
     308
    308309                return omittedAgents;
    309310        }
    310        
     311
    311312        /**
    312313         * Loads all the Methods that meet the requirements checked by MethodCheck
     
    470471                String nameWithCorrectCase = name;
    471472                name = name.toLowerCase();
    472                
     473
    473474                String fullClassName = AGENTS_PACKAGE + name;
    474475
     
    481482                                                + " " + parameters;
    482483                                fullClassName = AGENTS_PACKAGE + "writetree";
    483                                
     484
    484485                        } else if (name.endsWith("frame")) {
    485486                                parameters = name
     
    552553                        // create the JAG
    553554                        Agent toLaunch = (Agent) con.newInstance(params);
    554                        
     555
    555556                        LaunchAgent(toLaunch, source, clicked);
    556557
     
    559560                        throw new RuntimeException(nameWithCorrectCase
    560561                                        + "' is not an action or agent.");
    561                 } 
    562         }
    563        
     562                }
     563        }
     564
    564565        /**
    565566         * Launches an agent from a aleady instantiated object.
    566567         *
    567568         * @param agent
    568          *              The agent to launch. Must not be null.
     569         *            The agent to launch. Must not be null.
    569570         *
    570571         * @param source
    571          *              The calling frame that launched it. Must not be null.
     572         *            The calling frame that launched it. Must not be null.
    572573         *
    573574         * @param itemParam
    574          *              The item paremeter for the agent. Must not be null.
     575         *            The item paremeter for the agent.
    575576         *
    576577         * @throws NullPointerException
    577          *              if any of the arguments are null.
    578          */
    579         public static void LaunchAgent(
    580                         Agent agent,
    581                         Frame source,
    582                         Item itemParam) {
    583                
    584                 if (agent == null) throw new NullPointerException("agent");
    585                 if (source == null) throw new NullPointerException("source");
    586                 if (itemParam == null) throw new NullPointerException("itemParam");
    587                
     578         *             if any of the arguments are null.
     579         */
     580        public static void LaunchAgent(Agent agent, Frame source, Item itemParam) {
     581
     582                if (agent == null)
     583                        throw new NullPointerException("agent");
     584                if (source == null)
     585                        throw new NullPointerException("source");
     586                // if (itemParam == null) throw new NullPointerException("itemParam");
     587
    588588                String nameWithCorrectCase = agent.getClass().getSimpleName();
    589                
     589
    590590                try {
    591        
    592        
     591
    593592                        // create the JAG
    594593                        _Agent = agent;
    595        
     594
    596595                        Thread t = new Thread(_Agent);
    597596                        t.setPriority(Thread.MIN_PRIORITY);
     
    600599                                itemParam = FreeItems.getItemAttachedToCursor();
    601600                        }
    602        
     601
    603602                        // check for errors during initialisation
    604603                        if (!_Agent.initialise(source, itemParam)) {
     
    607606                                                + nameWithCorrectCase);
    608607                        }
    609        
     608
    610609                        // save the current frame (if necesssary)
    611610                        // TODO make this nicer... ie. make Format an action rather than an
    612611                        // agent and save frames only before running agents
    613                         if (!nameWithCorrectCase.equalsIgnoreCase("format") && !nameWithCorrectCase.equalsIgnoreCase("sort")) {
     612                        if (!nameWithCorrectCase.equalsIgnoreCase("format")
     613                                        && !nameWithCorrectCase.equalsIgnoreCase("sort")) {
    614614                                FrameUtils.LeavingFrame(source);
    615615                        }
    616        
     616
    617617                        if (_Agent.hasResultString()) {
    618618                                // Just run the agent on this thread... dont run it in the
  • trunk/src/org/expeditee/actions/MailActions.java

    r284 r305  
    66
    77import org.expeditee.agents.mail.MailSession;
     8import org.expeditee.gui.AttributeValuePair;
    89import org.expeditee.gui.DisplayIO;
    910import org.expeditee.gui.Frame;
     
    4849                return mail;
    4950        }
    50        
     51
    5152        public static Collection<Text> getRecentMail(int number) {
    5253                return getMail(null, null, number);
    5354        }
    54        
     55
     56        public static Collection<Text> getUnreadMail(Item clicked, int number) {
     57                if (clicked instanceof Text) {
     58                        AttributeValuePair avp = new AttributeValuePair(clicked
     59                                        .getText());
     60                        if (avp.hasPair() && avp.getValue().contains(MailSession.UNREAD_MESSAGE)) {
     61                                avp.setValue("0" + MailSession.UNREAD_MESSAGE + "s");
     62                                clicked.setText(avp.toString());
     63                                clicked.setActions(null);
     64                        }
     65                }
     66
     67                return getMail(Flag.SEEN, false, number);
     68        }
     69
    5570        public static Collection<Text> getUnreadMail() {
    5671                return getMail(Flag.SEEN, false, Integer.MAX_VALUE);
     
    6075                return getMail(null, null, Integer.MAX_VALUE);
    6176        }
    62        
     77
     78        public static Text getMail(int messageNo) {
     79                Text mailItem = MailSession.getInstance().getMail(
     80                                DisplayIO.getCurrentFrame(), FrameMouseActions.getPosition(),
     81                                messageNo - 1);
     82                // MessageBay.displayMessage(mailItems.size() + " messages read",
     83                // Color.green);
     84
     85                return mailItem;
     86        }
     87
    6388        public static Collection<Text> getMail() {
    6489                return getAllMail();
     
    6893                return getMail(Flag.RECENT, true, Integer.MAX_VALUE);
    6994        }
    70        
    71         private static Collection<Text> getMail(Flag flag, Boolean isPresent, int noOfMessages){
    72                 Collection<Text> mailItems = MailSession.getInstance().getMail(flag, isPresent,
    73                                 DisplayIO.getCurrentFrame(), FrameMouseActions.getPosition(), noOfMessages);
    74                 //MessageBay.displayMessage(mailItems.size() + " messages read", Color.green);
    75                
     95
     96        private static Collection<Text> getMail(Flag flag, Boolean isPresent,
     97                        int noOfMessages) {
     98                Collection<Text> mailItems = MailSession.getInstance().getMail(flag,
     99                                isPresent, DisplayIO.getCurrentFrame(),
     100                                FrameMouseActions.getPosition(), noOfMessages);
     101                // MessageBay.displayMessage(mailItems.size() + " messages read",
     102                // Color.green);
     103
    76104                return mailItems;
    77105        }
     
    95123                }
    96124        }
    97        
     125
    98126        public static String getMailCount() {
    99127                return getAllMailCount();
     
    121149                Frame replyFrame = DisplayIO.getCurrentFrame();
    122150                String titleText = frame.getTitle();
    123                 //Add Re on the end if it is not already there
     151                // Add Re on the end if it is not already there
    124152                if (titleText.length() >= 3
    125153                                && !"re:".equals(titleText.substring(0, 3).toLowerCase())) {
     
    128156                replyFrame.setTitle(titleText);
    129157                FrameKeyboardActions.Drop(null, false);
    130                
    131                 //Add a link to the original message
     158
     159                // Add a link to the original message
    132160                Text original = replyFrame.createNewText("@original");
    133161                original.setPosition(FrameMouseActions.getPosition());
     
    139167                to.addAction("MailTree");
    140168                FrameKeyboardActions.Drop(to, false);
    141                 DisplayIO.setCursorPosition(FrameMouseActions.MouseX, FrameMouseActions.MouseY + 15);
     169                DisplayIO.setCursorPosition(FrameMouseActions.MouseX,
     170                                FrameMouseActions.MouseY + 15);
    142171        }
    143172
  • trunk/src/org/expeditee/agents/mail/MailSession.java

    r298 r305  
    3232import org.expeditee.gui.Frame;
    3333import org.expeditee.gui.FrameCreator;
     34import org.expeditee.gui.FrameGraphics;
    3435import org.expeditee.gui.MessageBay;
    3536import org.expeditee.importer.FrameDNDTransferHandler;
     
    3839
    3940public class MailSession {
     41        public static final String UNREAD_MESSAGE = " unread message";
     42
    4043        public static boolean _autoConnect = false;
    4144
     
    154157                try {
    155158                        if (!_transport.isConnected()) {
    156                                 Text message = MessageBay.displayMessage("Connecting to SMTP server...");
     159                                Text message = MessageBay
     160                                                .displayMessage("Connecting to SMTP server...");
    157161                                _bConnecting = true;
    158162                                _transport.connect();
     
    168172                if (_mailServer != null && !_store.isConnected()) {
    169173                        try {
    170                                 Text message = MessageBay.displayMessage("Connecting to " + _mailServer
    171                                                 + "...");
     174                                Text message = MessageBay.displayMessage("Connecting to "
     175                                                + _mailServer + "...");
    172176                                _store.connect(_mailServer, _username, _password);
    173177
     
    185189                                        @Override
    186190                                        public void messagesAdded(MessageCountEvent e) {
    187                                                 MessageBay.displayMessage("New mail message!",
    188                                                                 Color.green);
     191                                                try {
     192                                                        MessageBay.displayMessage("New mail message!",
     193                                                                        null, Color.green, true, "getMail "
     194                                                                                        + _folder.getMessageCount());
     195                                                } catch (MessagingException e1) {
     196                                                        e1.printStackTrace();
     197                                                }
    189198                                                displayUnreadMailCount();
    190199                                        }
     
    204213                                                                _folder.getMessageCount();
    205214                                                                _folder.exists();
    206                                                                 //_folder.getUnreadMessageCount();
     215                                                                // _folder.getUnreadMessageCount();
    207216                                                        } catch (Exception e) {
    208217                                                                e.printStackTrace();
    209                                                                 MessageBay.errorMessage("Mail connection unavailable");
     218                                                                MessageBay
     219                                                                                .errorMessage("Mail connection unavailable");
    210220                                                                finalise();
    211221                                                                break;
     
    228238        }
    229239
    230         private void displayUnreadMailCount() {
     240        public void displayUnreadMailCount() {
    231241                int unreadCount = getUnreadCount();
    232                 Text text = new Text(-1, unreadCount + " unread message"
     242                Text text = new Text(-1, unreadCount + UNREAD_MESSAGE
    233243                                + (unreadCount == 1 ? "" : "s"), Color.BLUE, null);
    234                 text.addAction("getRecentMail " + unreadCount);
     244                if (unreadCount > 0)
     245                        text.addAction("getUnreadMail " + unreadCount);
    235246                MessageBay.displayMessage(text);
    236247        }
     
    342353        /**
    343354         * Closes the mail folders.
     355         *
    344356         * @return true if the folders needed to be closed.
    345357         */
     
    433445                        msgs = _folder.getMessages();
    434446
    435                         int start = Math.max(msgs.length - numberOfMessages, 0);
    436 
    437                         for (int msgNum = start; msgNum < msgs.length; msgNum++) {
     447                        int messagesRead = 0;
     448
     449                        for (int msgNum = msgs.length - 1; messagesRead < numberOfMessages
     450                                        && msgNum >= 0; msgNum--) {
    438451                                if (flag == null
    439452                                                || msgs[msgNum].getFlags().contains(flag) == isPresent) {
     
    443456                                                mailItems.add(newItem);
    444457                                                point.y += newItem.getBoundsHeight();
     458                                                messagesRead++;
    445459                                        }
    446460                                }
     
    450464                        e.printStackTrace();
    451465                }
    452                
     466
    453467                return mailItems;
     468        }
     469
     470        public Text getMail(Frame frame, Point point, int msgNum) {
     471                if (_folder == null)
     472                        return null;
     473
     474                // -- Get the message wrappers and process them --
     475                try {
     476                        Message[] msgs = _folder.getMessages();
     477                        return readMessage(msgs[msgNum], msgNum + 1, frame, point);
     478                } catch (MessagingException e) {
     479                        e.printStackTrace();
     480                }
     481
     482                return null;
    454483        }
    455484
     
    467496                                        // Create a frameCreator
    468497                                        final FrameCreator frames = new FrameCreator(frame
    469                                                         .getFramesetName(), frame.getPath(), subject, false, false);
     498                                                        .getFramesetName(), frame.getPath(), subject,
     499                                                        false, false);
    470500
    471501                                        frames.addText("@date: " + message.getSentDate(), null,
     
    529559                                        frames.save();
    530560                                        source.setLink(frames.getName());
     561                                        FrameGraphics.requestRefresh(true);
    531562                                } catch (MessagingException e) {
    532563                                        MessageBay.errorMessage("GetMail error: " + e.getMessage());
  • trunk/src/org/expeditee/gui/FrameIO.java

    r298 r305  
    171171        }
    172172
    173         public static String LoadPublicFrame(String frameName) {
    174                 assert (isValidFrameName(frameName));
    175                 Frame loaded = null;
    176                 String frameNameLower = frameName.toLowerCase();
    177                 // first try reading from cache
    178                 if (isCacheOn() && _Cache.containsKey(frameNameLower)) {
    179                         Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName
    180                                         + " from cache.");
    181                         loaded = _Cache.get(frameNameLower);
    182                 } else {
    183 
    184                         Logger.Log(Logger.SYSTEM, Logger.LOAD, "Loading " + frameName
    185                                         + " from disk.");
    186 
    187                         loaded = LoadFrame(PUBLIC_PATH, frameName);
    188                 }
    189 
    190                 String result = null;
    191                 // Get the contents of the frame to send to someone...
    192                 // TODO use a more efficient method to directly get the frame contents
    193                 // if possible
    194                 if (loaded != null) {
    195                         result = SaveFrame(loaded);
    196                 }
    197 
    198                 return result;
     173        public static BufferedReader LoadPublicFrame(String frameName) {
     174                String fullPath = FrameIO.getFrameFullPathName(PUBLIC_PATH, frameName);
     175               
     176                File frameFile = new File(fullPath);
     177                if (frameFile.exists() && frameFile.canRead()) {
     178                        try {
     179                                return new BufferedReader(new FileReader(frameFile));
     180                        } catch (FileNotFoundException e) {
     181                                e.printStackTrace();
     182                        }
     183                }
     184                return null;
    199185        }
    200186
     
    210196                }
    211197
    212                 if (loaded == null) {
     198                if (loaded == null && FrameShare.getInstance() != null) {
    213199                        loaded = FrameShare.getInstance().loadFrame(framename);
    214200                }
  • trunk/src/org/expeditee/network/FrameServer.java

    r298 r305  
    11package org.expeditee.network;
    22
     3import java.io.BufferedReader;
    34import java.io.IOException;
    45import java.net.DatagramPacket;
     
    1011
    1112public class FrameServer extends Thread {
     13
     14        private static final int MAX_PACKET_LENGTH = 2000;
    1215
    1316        private boolean _stop = false;
     
    2932
    3033        public void run() {
    31                 MessageBay.displayMessage("Serving frames on port " + socket.getLocalPort());
    32                
     34                MessageBay.displayMessage("Serving frames on port "
     35                                + socket.getLocalPort());
     36
    3337                while (!_stop) {
    3438                        try {
    35                                 byte[] buf = new byte[256];
     39                                byte[] buf = new byte[MAX_PACKET_LENGTH];
    3640
    3741                                // receive request
     
    3943                                socket.receive(packet);
    4044
    41                                 String frameName = new String(packet.getData());
     45                                String frameName = new String(packet.getData(), 0, packet
     46                                                .getLength());
     47                                MessageBay.displayMessage("Recieved request for " + frameName);
    4248
    4349                                // figure out response
    4450                                String dString = getFrame(frameName);
    4551
    46                                 if (dString != null) {
     52                                if (dString != null && dString.length() > 0) {
    4753                                        buf = dString.getBytes();
    4854
     
    6167
    6268        protected String getFrame(String frameName) {
    63                 return FrameIO.LoadPublicFrame(frameName);
     69                StringBuffer sb = new StringBuffer();
     70                BufferedReader br = FrameIO.LoadPublicFrame(frameName);
     71                if (br == null)
     72                        return null;
     73               
     74                String s = null;
     75                try {
     76                        while ((s = br.readLine()) != null && sb.length() < MAX_PACKET_LENGTH) {
     77                                sb.append(s).append('\n');
     78                        }
     79                } catch (IOException e) {
     80                        e.printStackTrace();
     81                }
     82
     83                return sb.toString();
    6484        }
    6585}
  • trunk/src/org/expeditee/network/FrameShare.java

    r298 r305  
    3838                // Set the settings
    3939                for (Text item : settingsFrame.getBodyTextItems(false)) {
    40                         String text = item.getText().toLowerCase().trim();
     40
    4141                        AttributeValuePair avp = new AttributeValuePair(item.getText());
     42                        if (avp.isAnnotation())
     43                                continue;
    4244
    43                         if (text.equals("server")) {
     45                        String attribute = avp.getAttributeOrValue().toLowerCase();
     46
     47                        if (attribute.equals("server")) {
    4448                                try {
    4549                                        if (!avp.hasPair()) {
     
    5963                        if (!avp.hasPair())
    6064                                continue;
    61                         String attribute = avp.getAttribute().toLowerCase();
    6265
    6366                        try {
     
    7780                }
    7881        }
    79        
     82
    8083        public void finalise() {
    8184                _server.close();
     
    119122                                        break;
    120123                                } catch (Exception e) {
     124                                        e.printStackTrace();
    121125                                }
    122126                        }
     
    125129
    126130                }
    127 
     131               
     132                if(result == null)
     133                        return null;
     134               
    128135                // Now read the frame from the file contents
    129136                FrameReader reader = new ExpReader(frameName);
Note: See TracChangeset for help on using the changeset viewer.