Kaydet (Commit) fa136f47 authored tarafından neilm's avatar neilm

Initial inport of Post to Newsgroup demo

üst b979c91b
import com.sun.star.uno.UnoRuntime;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.XComponentContext;
import drafts.com.sun.star.script.framework.XScriptContext;
import com.sun.star.util.XStringSubstitution;
import javax.mail.*;
import javax.activation.*;
import java.io.*;
public class MimeConfiguration
{
// Office Installation path
private static String instPath = "";
public static boolean createFiles( XScriptContext xsc )
{
try
{
XComponentContext xcc = xsc.getComponentContext();
XMultiComponentFactory xmf = xcc.getServiceManager();
Object pathSub = xmf.createInstanceWithContext( "com.sun.star.comp.framework.PathSubstitution", xcc );
XStringSubstitution stringSub = ( XStringSubstitution ) UnoRuntime.queryInterface( XStringSubstitution.class, pathSub );
instPath = stringSub.getSubstituteVariableValue( "$(inst)" );
}
catch( com.sun.star.beans.UnknownPropertyException upe )
{
System.out.println( "com.sun.star.beans.UnknownPropertyException" );
upe.printStackTrace();
}
catch( com.sun.star.uno.Exception e )
{
System.out.println( "com.sun.star.uno.Exception" );
e.printStackTrace();
}
writeMailCap();
writeMimeTypes();
// ToDo: include status feedback to StatusWindow
return true;
}
private static void writeMailCap()
{
String mailcapPath = getConfigDir() + System.getProperty( "file.separator" ) + "mailcap";
try
{
if( ! new File( java.net.URLDecoder.decode( mailcapPath ) ).exists() )
{
//System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) );
File mailcapFile = new File( mailcapPath );
FileWriter out = new FileWriter( mailcapFile );
String[] lines = getMailcapText();
for( int i=0; i<lines.length; i++ )
{
out.write( lines[i], 0, lines[i].length() );
}
out.close();
}
else
{
//System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mailcapPath ) );
}
// use prog dir, if not there then java.io to create/write new file
MailcapCommandMap map = new MailcapCommandMap( mailcapPath );
CommandMap.setDefaultCommandMap ( map );
}
catch( IOException ioe )
{
ioe.printStackTrace();
}
catch( Exception e )
{
e.printStackTrace();
}
}
private static String[] getMailcapText()
{
String[] mailcapText = {
"#\n",
"# Default mailcap file for the JavaMail System.\n",
"#\n",
"# JavaMail content-handlers:\n",
"#\n",
"text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain\n",
"text/html;; x-java-content-handler=com.sun.mail.handlers.text_html\n",
"text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml\n",
"image/gif;; x-java-content-handler=com.sun.mail.handlers.image_gif\n",
"image/jpeg;; x-java-content-handler=com.sun.mail.handlers.image_jpeg\n",
"multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed\n",
"message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822\n"
};
return mailcapText;
}
private static void writeMimeTypes()
{
String mimetypesPath = getConfigDir() + System.getProperty( "file.separator" ) + "mimetypes.default";
try
{
if( ! new File( java.net.URLDecoder.decode( mimetypesPath ) ).exists() )
{
//System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) );
File mimetypesFile = new File( mimetypesPath );
FileWriter out = new FileWriter( mimetypesFile );
String[] lines = getMimeTypesText();
for( int i=0; i<lines.length; i++ )
{
out.write( lines[i], 0, lines[i].length() );
}
out.close();
}
else
{
//System.out.println( "URLDecoder: " + java.net.URLDecoder.decode( mimetypesPath ) );
}
MimetypesFileTypeMap mimeTypes = new MimetypesFileTypeMap( mimetypesPath );
FileTypeMap.setDefaultFileTypeMap( mimeTypes );
}
catch( IOException ioe )
{
ioe.printStackTrace();
}
catch( Exception e )
{
e.printStackTrace();
}
}
private static String[] getMimeTypesText()
{
String[] mimesText = {
"#\n",
"# A simple, old format, mime.types file\n",
"#\n",
"text/html html htm HTML HTM\n",
"text/plain txt text TXT TEXT\n",
"image/gif gif GIF\n",
"image/ief ief\n",
"image/jpeg jpeg jpg jpe JPG\n",
"image/tiff tiff tif\n",
"image/x-xwindowdump xwd\n",
"application/postscript ai eps ps\n",
"application/rtf rtf\n",
"application/x-tex tex\n",
"application/x-texinfo texinfo texi\n",
"application/x-troff t tr roff\n",
"audio/basic au\n",
"audio/midi midi mid\n",
"audio/x-aifc aifc\n",
"audio/x-aiff aif aiff\n",
"audio/x-mpeg mpeg mpg\n",
"audio/x-wav wav\n",
"video/mpeg mpeg mpg mpe\n",
"video/quicktime qt mov\n",
"video/x-msvideo avi\n"
};
return mimesText;
}
private static String getConfigDir()
{
// mailcap file must be written to the Office user/config directory
// instPath is a URL, needs to be converted to a system pathname
String config = instPath + "/user/config";
String configNonURL = "";
if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
{
// Windows
// removes "file:///"
int start = 8;
configNonURL = config.substring( start, config.length() );
// Convert forward to back-slashes
while( configNonURL.indexOf( "/" ) != -1 )
{
int fSlash = configNonURL.indexOf( "/" );
String firstPart = configNonURL.substring( 0, fSlash );
String secondPart = configNonURL.substring( fSlash + 1, configNonURL.length() );
configNonURL = firstPart + "\\" + secondPart;
}
}
else
{
// Unix/Linux
// removes "file://"
int start = 7;
configNonURL = config.substring( start, config.length() );
}
return configNonURL;
}
}
public class NewsGroup
{
private String hostname = "";
private String newsgroupName = "";
public NewsGroup( String host, String group )
{
hostname = host;
newsgroupName = group;
}
public String getHostName()
{
return hostname;
}
public String getNewsgroupName()
{
return newsgroupName;
}
}
import javax.mail.*;
import javax.mail.internet.*;
import com.msoft.mail.provider.nntp.NNTPTransport;
import java.util.Properties;
import java.io.*;
import javax.activation.*;
public class Sender
{
// Constructor params:
private StatusWindow status = null;
private OfficeAttachment attachments = null;
private String replyto = "";
private String subject = "";
private String comment = "";
private String hostname = "";
private String newsgroup = "";
private String statusLine = "";
public Sender( StatusWindow sw, OfficeAttachment attach, String reply,
String sub, String com, String host, String group )
{
status = sw;
attachments = attach;
replyto = reply;
subject = sub;
comment = com;
hostname = host;
newsgroup = group;
}
public boolean sendMail()
{
int statusPos = 5;
try
{
attachments.createTempDocs();
// Property for any information
Properties props = new Properties();
// Create unique session (null is unused authenticator info)
statusLine = "Creating unique session";
status.setStatus( statusPos, statusLine ); // 5
Session session = Session.getInstance( props, null );
// Create message
statusPos++; // 6
statusLine = "Creating message";
status.setStatus( statusPos, statusLine );
MimeMessage message = new MimeMessage( session );
message.setFrom( new InternetAddress( replyto ) );
message.setSubject( subject );
message.setText( comment );
message.addHeader( "Newsgroups", newsgroup );
// Buildup bodypart with text and attachments
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText( comment );
multipart.addBodyPart( messageBodyPart );
statusPos++; // 7
statusLine = "Adding attachment(s)";
status.setStatus( statusPos, statusLine );
File attachs[] = attachments.getAttachments();
for(int i=0; i < attachs.length; i++ )
{
//System.out.println( "Adding file: " + attachs[i].getName() );
messageBodyPart = new MimeBodyPart();
DataSource filesource = new FileDataSource( attachs[i] );
messageBodyPart.setDataHandler( new DataHandler( filesource ));
messageBodyPart.setFileName( attachs[i].getName() );
multipart.addBodyPart( messageBodyPart );
}
// Add multipart to mail
message.setContent( multipart );
// Create and send NNTP transport
statusPos += 2; // 9
statusLine = "Creating NNTP transport";
status.setStatus( statusPos, statusLine );
Transport transport = new NNTPTransport( session, new URLName( "news:" + newsgroup ));
// Null parameters are for user name and password
statusPos++; // 10
statusLine = "Connecting to mail server";
status.setStatus( statusPos, statusLine );
transport.connect( hostname, null, null );
statusPos++; // 11
statusLine = "Sending message";
status.setStatus( statusPos, statusLine );
transport.sendMessage( message, message.getAllRecipients() );
statusPos++; // 12
statusLine = "Closing transport";
status.setStatus( statusPos, statusLine );
transport.close();
// Clean up when finished
attachments.removeTempDocs();
return true;
}
catch( MessagingException me )
{
if( statusPos == 10 )
{
statusLine = "Error connecting (User authentication?)";
}
status.setStatus( statusPos, statusLine );
System.out.println( "Error sending message: ");
me.printStackTrace();
return false;
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class StatusWindow extends JFrame
{
private JProgressBar progressBar = null;
private JTextField statusLabel = null;
private JButton cancelButton = null;
private JFrame statusWindow = null;
private PostNewsgroup mainWindow = null;
private final int MAXPROGRESS = 13;
private final int MINPROGRESS = 0;
public StatusWindow( PostNewsgroup mw, String title, int parentX, int parentY )
{
this.setTitle( title );
this.setLocation( parentX + 100, parentY + 100 );
statusWindow = this;
mainWindow = mw;
mainWindow.enableButtons( false );
statusWindow.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent event ) {
mainWindow.enableButtons( true );
}
});
progressBar = new JProgressBar();
progressBar.setStringPainted( true );
progressBar.setMaximum( MAXPROGRESS );
progressBar.setMinimum( MINPROGRESS );
progressBar.setSize( 30, 400 );
JLabel progLabel = new JLabel( "Progress:" );
JPanel progressPanel = new JPanel();
progressPanel.setLayout( new BorderLayout( 10, 0 ) );
progressPanel.add( progLabel, "West" );
progressPanel.add( progressBar, "East" );
statusLabel = new JTextField();
statusLabel.setColumns( 25 );
statusLabel.setEditable( false );
statusLabel.setBorder( null );
//statusLabel.setBorder( LineBorder.createGrayLineBorder() );
JPanel statusPanel = new JPanel();
//statusPanel.setBorder( LineBorder.createBlackLineBorder() );
statusPanel.setLayout( new BorderLayout() );
statusPanel.add( statusLabel, "West" );
cancelButton = new JButton( "Cancel" );
cancelButton.setSize( 30, 100 );
cancelButton.setEnabled( false );
cancelButton.addActionListener( new ActionListener()
{
public void actionPerformed( ActionEvent event ) {
// cancelling actions
mainWindow.enableButtons( true );
statusWindow.dispose();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new BorderLayout( 0, 5 ) );
buttonPanel.add( cancelButton, "East" );
buttonPanel.add( new JSeparator( SwingConstants.HORIZONTAL ), "North" );
Container container = getContentPane();
container.setLayout( new GridBagLayout() );
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets( 15, 15, 10, 15 );
container.add( progressPanel, constraints );
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets( 10, 15, 10, 15 );
container.add( statusPanel, constraints );
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.insets = new Insets( 10, 15, 5, 15 );
container.add( buttonPanel, constraints );
this.pack();
this.setResizable( false );
//this.setVisible( true );
}
public void setStatus( int progress, String status )
{
progressBar.setValue( progress );
statusLabel.setText( status );
statusLabel.setToolTipText( status );
if( progress == MAXPROGRESS )
{
cancelButton.setEnabled( true );
cancelButton.setText( "Close" );
}
update( getGraphics() );
}
public void enableCancelButton( boolean enable )
{
if( enable )
{
cancelButton.setEnabled( true );
cancelButton.setText( "Close" );
}
else
{
cancelButton.setEnabled( false );
cancelButton.setText( "Cancel" );
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment