i18n
[jalview.git] / src / jalview / gui / UserQuestionnaireCheck.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import jalview.util.MessageManager;
21
22 import java.io.*;
23 import java.net.*;
24
25 import javax.swing.*;
26
27 public class UserQuestionnaireCheck implements Runnable
28 {
29   /**
30    * Implements the client side machinery for detecting a new questionnaire,
31    * checking if the user has responded to an existing one, and prompting the
32    * user for responding to a questionnaire. This is intended to work with the
33    * perl CGI scripts checkresponder.pl and questionnaire.pl
34    */
35   String url = null;
36
37   UserQuestionnaireCheck(String url)
38   {
39     if (url.indexOf("questionnaire.pl") == -1)
40     {
41       jalview.bin.Cache.log
42               .error("'"
43                       + url
44                       + "' is an Invalid URL for the checkForQuestionnaire() method.\n"
45                       + "This argument is only for questionnaires derived from jalview's questionnaire.pl cgi interface.");
46     }
47     else
48     {
49       this.url = url;
50     }
51   }
52
53   String qid = null, rid = null;
54
55   private boolean checkresponse(URL qurl) throws Exception
56   {
57     jalview.bin.Cache.log.debug("Checking Response for : " + qurl);
58     boolean prompt = false;
59     // see if we have already responsed to this questionnaire or get a new
60     // qid/rid pair
61     BufferedReader br = new BufferedReader(new InputStreamReader(
62             qurl.openStream()));
63     String qresp;
64     while ((qresp = br.readLine()) != null)
65     {
66       if (qresp.indexOf("NOTYET:") == 0)
67       {
68         prompt = true; // not yet responded under that ID
69       }
70       else
71       {
72         if (qresp.indexOf("QUESTIONNAIRE:") == 0)
73         {
74           // QUESTIONNAIRE:qid:rid for the latest questionnaire.
75           int p = qresp.indexOf(':', 14);
76           if (p > -1)
77           {
78             rid = null;
79             qid = qresp.substring(14, p);
80             if (p < (qresp.length() - 1))
81             {
82               rid = qresp.substring(p + 1);
83               prompt = true; // this is a new qid/rid pair
84             }
85           }
86         }
87       }
88     }
89     return prompt;
90   }
91
92   public void run()
93   {
94     if (url == null)
95     {
96       return;
97     }
98     boolean prompt = false;
99     try
100     {
101       // First - check to see if wee have an old questionnaire/response id pair.
102       String lastq = jalview.bin.Cache.getProperty("QUESTIONNAIRE");
103       if (lastq == null)
104       {
105         prompt = checkresponse(new URL(url
106                 + (url.indexOf('?') > -1 ? "&" : "?") + "checkresponse=1"));
107       }
108       else
109       {
110         String qurl = url + (url.indexOf('?') > -1 ? "&" : "?")
111                 + "checkresponse=1";
112         // query the server with the old qid/id pair
113         String qqid = lastq.indexOf(':') > -1 ? lastq.substring(0,
114                 lastq.indexOf(':')) : null;
115         if (qqid != null && qqid != "null" && qqid.length() > 0)
116         {
117           qurl += "&qid=" + qqid;
118           qid = qqid;
119           String qrid = lastq.substring(lastq.indexOf(':') + 1); // retrieve
120           // old rid
121           if (qrid != null && !qrid.equals("null"))
122           {
123             rid = qrid;
124             qurl += "&rid=" + qrid;
125           }
126         }
127         // see if we have already responsed to this questionnaire.
128         prompt = checkresponse(new URL(qurl));
129       }
130       if (qid != null && rid != null)
131       {
132         // Update our local property cache with latest qid and rid
133         jalview.bin.Cache.setProperty("QUESTIONNAIRE", qid + ":" + rid);
134       }
135       if (prompt)
136       {
137         String qurl = url + (url.indexOf('?') > -1 ? "&" : "?") + "qid="
138                 + qid + "&rid=" + rid;
139         jalview.bin.Cache.log.info("Prompting user for questionnaire at "
140                 + qurl);
141         int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
142                 MessageManager.getString("label.jalview_new_questionnaire"),
143                 MessageManager.getString("label.jalview_user_survey"), JOptionPane.YES_NO_OPTION,
144                 JOptionPane.QUESTION_MESSAGE);
145
146         if (reply == JOptionPane.YES_OPTION)
147         {
148           jalview.bin.Cache.log.debug("Opening " + qurl);
149           jalview.util.BrowserLauncher.openURL(qurl);
150         }
151       }
152     } catch (Exception e)
153     {
154       jalview.bin.Cache.log.warn("When trying to access questionnaire URL "
155               + url, e);
156     }
157   }
158
159 }