//http://office09.googlecode.com/svn-history/r491/trunk/OfficeInCloudWord2007/WordLocalView.cs // http://office09.googlecode.com/svn/trunk/ using System; using System.Collections; using System.Linq; using System.Text; using System.Globalization; using LocalView; using DataType; using Google.GData.Client; using Google.Contacts; using Google.Documents; using Google.GData.Documents; using System.IO; using Google.GData.AccessControl; using Word = Microsoft.Office.Interop.Word; using VSTO = Microsoft.Office.Tools.Word; using Microsoft.Office.Tools.Word.Extensions; using Utilities; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; namespace OfficeInCloudWord2007 { public class WordLocalView : ILocalView { private bool isLoggedIn; public delegate void StateNotify(string key); private global::System.Object missing = global::System.Type.Missing; public Word.Application m_App; public override event EventHandler<Credentials> Login; public override event EventHandler<FeedWrapper<Contact>> GetContacts; public override event EventHandler<FeedWrapper<Document>> GetDocuments; public override event EventHandler<FeedWrapper<Document>> GetFolders; public override event EventHandler<DocumentInfo> UploadDocument; public override event EventHandler<DocumentInfo> UpdateDocument; public override event EventHandler<DownloadInfo> DownloadDocument; public override event EventHandler<ShareInfoCollection> ShareDocument; public override event EventHandler<ShareInfoCollection> GetDocumentAcl; public override event EventHandler<DocumentInfo> LookForDocument; public override event EventHandler<DocumentInfo> OpenDocumentInCloud; public override event EventHandler<ContactInfo> AddContact; private DbOperations m_dbOperations; public StateNotify m_LoadState; public StateNotify m_StoreState; public WordLocalView(Word.Application App, DbOperations dbOperations) { m_App = App; m_DocInfo = null; m_dbOperations = dbOperations; isLoggedIn = false; } public override string GetDocumentPath() { return m_App.ActiveDocument.FullName; } public override string GetDocumentName() { return m_App.ActiveDocument.Name; } public override void DoLogin(Credentials cred) { if (Login != null) Login(this, cred); isLoggedIn = true; } public override void DoDownloadDocument(DownloadInfo info) { if (DownloadDocument != null) DownloadDocument(this, info); } public override void DoGetContacts(FeedWrapper<Contact> feed) { if (GetContacts != null) GetContacts(this, feed); } public void DoGetContactsAsync(NotifyDelegate<Contact> Notify) { if (GetContacts != null) { DoIterationASync<Contact> Iteration = new DoIterationASync<Contact>(this.GetContacts, Notify, this.dummy); Iteration.StartAction(); } } public override void DoGetDocuments(FeedWrapper<Document> feed) { if (GetDocuments != null) GetDocuments(this, feed); } public void DoGetDocumentsAsync(NotifyDelegate<Document> Notify) { if (GetDocuments != null) { DoIterationASync<Document> Iteration = new DoIterationASync<Document>(this.GetDocuments, Notify, this.dummy); Iteration.StartAction(); } } private void saveDocument() { Word.WdAlertLevel wd = m_App.DisplayAlerts; try { m_App.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; string fname = m_App.ActiveDocument.FullName; string docName1 = m_App.ActiveDocument.Name; if (!System.IO.Path.IsPathRooted(fname)) { object path = Utilities.Path.documentPath + "\\" + m_App.ActiveDocument.FullName; m_App.ActiveDocument.SaveAs(ref path, ref missing, ref missing, //password ref missing, //writeRefPassword ref missing, //readOnlyRecommended ref missing, //createBackup ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); } else m_App.ActiveDocument.Save(); } finally { m_App.DisplayAlerts = wd; } } public override void DoSaveDocumentInCloud() { saveDocument(); if (UploadDocument != null) { m_DocInfo = new DocumentInfo(m_App.ActiveDocument.FullName, m_App.ActiveDocument.Name); UploadDocument(this, m_DocInfo); m_dbOperations.DoRegisterLocalCopy(m_DocInfo); } } public override void Merge(Document dt, ILocalView.CollisionWinner winner) { Document.DownloadType type = Document.DownloadType.doc; DownloadInfo info = new DownloadInfo(dt, type); DownloadDocument(this, info); //get new DocumentEnty //DoLookupRemoteCopy(m_DocInfo); Word.Document localWb = m_App.ActiveDocument; string tmpPath = localWb.FullName + "_tmp.doc"; FileInfo fileInfo = new FileInfo(tmpPath); FileStream stream = fileInfo.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); CommonOperations.DoSaveContent(stream, info.m_Stream); MergeContent(tmpPath); fileInfo.Delete(); //throw new NotImplementedException(); } private void MergeContent(string serverCopyPath) { object MergeTarget = Word.WdMergeTarget.wdMergeTargetCurrent; object DetectFormatChanges = true; object UseFormattingFrom = Word.WdUseFormattingFrom.wdFormattingFromCurrent; object AddToRecentFiles = true; m_App.ActiveDocument.Merge(serverCopyPath, ref MergeTarget, ref DetectFormatChanges, ref UseFormattingFrom, ref AddToRecentFiles); m_App.ActiveDocument.Save(); } public override void DoLookupRemoteCopy(DocumentInfo info) { //at first look for document id in db, if no document then exception happens m_dbOperations.DoLookupRemoteCopy(info); if (info.m_RemoteId == null) return; if (LookForDocument != null) LookForDocument(this, info); } public override void DoUpdateDocument() { if (UpdateDocument != null) { m_App.ActiveDocument.Save(); UpdateDocument(this, m_DocInfo); } //try{ // if (UpdateDocument != null) // { // m_App.ActiveDocument.Save(); // UpdateDocument(this, m_DocInfo); // } //} //catch(GDataRequestException e) //{ // //create later lookup for type depending extension using DocumentEntry.IsSpreadsheet and so on // Document.DownloadType type = Document.DownloadType.doc; // Document dt = new Document(); // dt.AtomEntry = m_DocInfo.m_DocEntry; // DownloadInfo info = new DownloadInfo(dt, type); // DownloadDocument(this, info); // //get new DocumentEnty // DoLookupRemoteCopy(m_DocInfo); // Word.Document wb = m_App.ActiveDocument; // string tmpPath = wb.Path + "\\" + wb.Name + "_tmp.doc"; // FileInfo fileInfo = new FileInfo(tmpPath); // FileStream stream = fileInfo.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); // CommonOperations.DoSaveDocumentContent(stream, info.m_Stream); // MergeDocumentWith(tmpPath); // fileInfo.Delete(); // //try to update document again // DoUpdateDocument(); //} } private void BuildListOfUsers(IEnumerable users, Permission perm, AclFeed feed) { string permStr = null; switch (perm) { case Permission.reader: permStr = "reader"; break; case Permission.writer: permStr = "writer"; break; } foreach (object obj in users) { AclEntry entry = new AclEntry(); entry.Scope = new AclScope(); entry.Scope.Type = AclScope.SCOPE_USER; //parse the email string value = obj.ToString(); if(value.Contains('(')) { value = value.Split(new char[] {'(', ')'})[1]; } entry.Scope.Value = value; entry.Role = new AclRole(permStr); feed.Entries.Add(entry); } } private void dummy() {} private void RefreshEtag() { DoLookupRemoteCopy(m_DocInfo); } public override void UpdateDocumentInfo() { m_DocInfo = new DocumentInfo(m_App.ActiveDocument.FullName, m_App.ActiveDocument.Name); DoLookupRemoteCopy(m_DocInfo); } public override void DoShareDocumentsAsync(IEnumerable users, Permission perm) { if (ShareDocument != null) { ShareInfoCollection info = new ShareInfoCollection(m_DocInfo.m_DocEntry); BuildListOfUsers(users, perm, info.m_Feed); DoOperationASync<ShareInfoCollection> operation = new DoOperationASync<ShareInfoCollection>(this.ShareDocument, info, this.RefreshEtag); operation.StartAction(); } } public override void DoOpenLocalCopy(string filePath) { object fileName = filePath; object readOnly = false; object isVisible = true; // Here is the way to handle parameters you don't care about in .NET object missing = System.Reflection.Missing.Value; try { Word.Document wb = m_App.Documents.Open( ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); wb.Save(); wb.Activate(); //m_App.ActiveDocument.TrackRevisions = true; } catch (Exception ex) { Utilities.TraceWrapper.LogException("DoOpenLocalCopyShared error: " + ex.Message, ex); } } public override string GetInviteUri() { return m_DocInfo.m_DocEntry.AlternateUri.Content; } public override void DoOpenDocumentInCloud(DocumentInfo info) { if (OpenDocumentInCloud != null) OpenDocumentInCloud(this, info); } public override void DoGetDocumentAcl(ShareInfoCollection info) { if (GetDocumentAcl != null) GetDocumentAcl(this, info); } //////////////////////////////////////////////////////// // Temporary stubs /////////////////////////////////////////////////////// public override bool IsDocumentOpened(string path) { foreach (Word.Document dc in m_App.Documents) if (dc.FullName == path) return true; return false; } public override void CloseDocument(string path) { foreach (Word.Document dc in m_App.Documents) if (dc.FullName == path) { object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; object route = false; dc.Close(ref saveChanges, ref missing, ref route); } } public override void ActivateDocument(string path) { foreach (Word.Document dc in m_App.Documents) if (dc.FullName == path) dc.Activate(); } public override string SupportedFormats() { return "Word|*.doc|Word 2007|*.docx"; } public override Document.DocumentType DocumentType() { return Document.DocumentType.Document; } public override Document.DownloadType DownloadType() { return Document.DownloadType.doc; } public override string GetContentText() { return m_App.ActiveDocument.Content.Text; } public override void SmartTagActions(Object tag) { m_App.ActiveDocument.GetVstoObject().VstoSmartTags.Add(tag as VSTO.SmartTag); m_App.ActiveDocument.RecheckSmartTags(); // m_App.ActiveDocument.GetVstoObject().SmartTags.ReloadRecognizers(); m_App.SmartTagRecognizers.ReloadRecognizers(); } public override void HighlightSmartTags(string s) { foreach (Word.Range word in m_App.ActiveDocument.Words) //If we trim, we need to highlight less than the size of the word otherwise the space after //the word is highlighted as well. if (string.Compare(word.Text.Trim(), s, true, CultureInfo.InvariantCulture) == 0) word.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdYellow; } public override bool IsLoggedIn() { return isLoggedIn; } //this method is required since user can upload both xls, xlsx, but download only one .xls //so it's possible that downloaded .xls file but with extension .xlsx due local copy is .xlsx public override void CastContentToExtension(string docPath, Document.DownloadType type) { //it means that we downloaded xls but with extension xlsx, we need convert content to the format xlsx if ((Document.DownloadType.doc == type) && docPath.EndsWith(".docx")) { Word.WdAlertLevel wd = m_App.DisplayAlerts; try { m_App.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; //rename file to the correct extension string correctedPath = docPath + System.Guid.NewGuid().ToString() + ".doc"; FileInfo ab = new FileInfo(docPath); ab.MoveTo(correctedPath); //change its format to the required object file = correctedPath; Word.Document tmp = m_App.Documents.Open(ref file, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); object soureceFile = docPath; object format = Word.WdSaveFormat.wdFormatDocumentDefault; tmp.SaveAs(ref soureceFile, ref format, ref missing, //password ref missing, //writeRefPassword ref missing, //readOnlyRecommended ref missing, //createBackup ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); object saveChanges = Word.WdSaveOptions.wdSaveChanges; object route = false; tmp.Close(ref saveChanges, ref missing, ref route); ab.Delete(); } catch (Exception e) { Utilities.TraceWrapper.LogException("CastContentToExtension error: " + e.Message, e); } finally { m_App.DisplayAlerts = wd; } } } public override void InsertSearchResult(string[] result) { Word.Selection currentSelection = this.m_App.Selection; // Store the user's current Overtype selection bool userOvertype = this.m_App.Options.Overtype; // Make sure Overtype is turned off. if (this.m_App.Options.Overtype) { this.m_App.Options.Overtype = false; } // Test to see if selection is an insertion point. if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP) { TypeText(currentSelection, result); } else if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal) { // Move to start of selection. if (this.m_App.Options.ReplaceSelection) { object direction = Word.WdCollapseDirection.wdCollapseStart; currentSelection.Collapse(ref direction); } TypeText(currentSelection, result); } else { // Do nothing. } // Restore the user's Overtype selection this.m_App.Options.Overtype = userOvertype; } private void TypeText(Word.Selection currentSelection, string[] results) { currentSelection.TypeText("Title: " + results[0]); currentSelection.TypeParagraph(); currentSelection.TypeText("Description: "); currentSelection.TypeParagraph(); currentSelection.TypeText(results[1]); currentSelection.TypeParagraph(); currentSelection.TypeText("Public profile: "); object s = results[2]; m_App.ActiveDocument.Hyperlinks._Add( currentSelection.Range, ref s, ref missing ); } public override void DoAddContact(ContactInfo contact) { if (AddContact != null) AddContact(this, contact); } static void SetHeadings(Microsoft.Office.Interop.Word.Cell tblCell, string text) { tblCell.Range.Text = text; tblCell.Range.Font.Bold = 1; tblCell.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; } public override void InsertSearchResultEx(string[] result) { Word.Selection currentSelection = this.m_App.Selection; Word.Range rng = currentSelection.Range; //VSTO.ControlCollection controls = m_App.ActiveDocument.GetVstoObject().Controls; object s = result[3]; rng.Text = result[1]; Word.Hyperlink hl = rng.Hyperlinks._Add(rng, ref s, ref missing); rng.Start = rng.End + 1; VSTO.Controls.PictureBox pb = m_App.ActiveDocument.GetVstoObject().Controls.AddPictureBox(rng, 15, 15, System.Guid.NewGuid().ToString()); /*object oType = Word.WdFieldType.wdFieldComments; //.wdFieldAddin; object oResult = ""; object oBool = true; foreach (string str in result) { oResult += str + "|"; } Word.Field fd = m_App.ActiveDocument.Fields.Add(rng, ref oType, ref oResult, ref oBool); */ pb.Image = Properties.Resources.Loop_small; pb.Tag = result; pb.MouseEnter += new EventHandler(pb_MouseEnter); pb.MouseLeave += new EventHandler(pb_MouseLeave); } public override void HighLightsTags(string[] result) { try { //VSTO.ControlCollection controls = m_App.ActiveDocument.GetVstoObject().Controls; Word.Range rng = m_App.ActiveDocument.Content; rng.Find.ClearFormatting(); object findText = result[1]; object oTrue = true; object oFalse = false; object oFindStop = Word.WdFindWrap.wdFindStop; int pos = rng.End; rng.Find.Execute(ref findText, ref oFalse, ref oTrue, ref oTrue, ref oFalse, ref oFalse, ref oTrue, ref oFindStop, ref oFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); while (rng.Find.Found) { object s = result[3]; Word.Hyperlink hl = rng.Hyperlinks._Add(rng, ref s, ref missing); rng.Start = rng.End + 1; VSTO.Controls.PictureBox pb = m_App.ActiveDocument.GetVstoObject().Controls.AddPictureBox(rng, 15, 15, System.Guid.NewGuid().ToString()); pb.Image = Properties.Resources.Loop_small; pb.Tag = result; pb.MouseEnter += new EventHandler(pb_MouseEnter); pb.MouseLeave += new EventHandler(pb_MouseLeave); pos = rng.End; rng = m_App.ActiveDocument.Content; rng.Start = pos + 1; rng.Find.Execute(ref findText, ref oFalse, ref oTrue, ref oTrue, ref oFalse, ref oFalse, ref oTrue, ref oFindStop, ref oFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); if (pos >= rng.Start) return; } } catch (Exception e) { } } public override void TestAddContentcontrol(string[] results) { Word.Range rng = m_App.ActiveDocument.Content; VSTO.Controls.PictureBox pb = m_App.ActiveDocument.GetVstoObject().Controls.AddPictureBox(rng, 15, 15, "picture"); pb.Image = Properties.Resources.Loop_small; pb.Tag = results; //pb.MouseHover += new EventHandler(lb_MouseHover); pb.MouseEnter +=new EventHandler(pb_MouseEnter); pb.MouseLeave +=new EventHandler(pb_MouseLeave); } private CardDialog card = new CardDialog(); private void pb_MouseEnter(object sender, EventArgs e) { //int i = 10; if (!card.Visible) { VSTO.Controls.PictureBox pb = (VSTO.Controls.PictureBox)sender; card.Width = 500; card.Height = 300; card.ControlBox = false; card.StartPosition = System.Windows.Forms.FormStartPosition.Manual; card.Location = new System.Drawing.Point( System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y); card.lbTitle.Text = ((string[])pb.Tag)[1]; card.txtDescription.Text = ((string[])pb.Tag)[2]; card.lbWebsite.Text = ((string[])pb.Tag)[3]; card.lbCategory.Text = ((string[])pb.Tag)[0]; card.Show(); } } private void pb_MouseLeave(object sender, EventArgs e) { //int i = 10; VSTO.Controls.PictureBox pb = (VSTO.Controls.PictureBox)sender; card.Hide(); } public override string GetHTMLDocumentContent() {/* object prev_path = m_App.ActiveDocument.FullName; //Word.XMLNodes nodes = m_App.ActiveDocument.GetVstoObject().XMLNodes; object path = System.IO.Path.GetTempPath() //+ m_App.ActiveDocument.Name.Split(new char[]{'.'})[0] + Guid.NewGuid() + ".html"; object oFormat = Word.WdSaveFormat.wdFormatFilteredHTML; object oBool = true; m_App.ActiveDocument.SaveAs(ref path, ref oFormat, ref missing, //password ref missing, //writeRefPassword ref missing, //readOnlyRecommended ref missing, //createBackup ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); m_App.ActiveDocument.Close(ref oBool, ref missing, ref oBool); m_App.Documents.Open(ref prev_path, ref missing, //password ref missing, //writeRefPassword ref missing, //readOnlyRecommended ref missing, //createBackup ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); string content; using (FileStream fs = File.OpenRead((string)path)) { StreamReader rd = new StreamReader(fs, Encoding.UTF8); content = rd.ReadToEnd(); } return content; * */ /* m_App.ActiveDocument.Content.Copy(); object oDocType = Word.WdDocumentType.wdTypeDocument; object oBool = false; Word.Document doc = m_App.Documents.Add(ref missing, ref missing, ref oDocType, ref oBool); doc.Content.Paste(); object oFormat = Word.WdSaveFormat.wdFormatFilteredHTML; oBool = true; object path = System.IO.Path.GetTempPath() //+ m_App.ActiveDocument.Name.Split(new char[]{'.'})[0] + Guid.NewGuid() + ".html"; doc.SaveAs(ref path, ref oFormat, ref missing, //password ref missing, //writeRefPassword ref missing, //readOnlyRecommended ref missing, //createBackup ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.Close(ref oBool, ref missing, ref oBool); string content; using (FileStream fs = File.OpenRead((string)path)) { StreamReader rd = new StreamReader(fs, Encoding.UTF8); content = rd.ReadToEnd(); } return content; ** */ /* Word.XMLNodes nodes = m_App.ActiveDocument.GetVstoObject().XMLNodes; XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(m_App.ActiveDocument.WordOpenXML); XslTransform transform = new XslTransform(); XmlTextReader xls = new XmlTextReader(new StringReader(Properties.Resources.OpenXML)); XsltArgumentList list = new XsltArgumentList(); StringWriter res = new StringWriter(); transform.Load(xls); XmlTextWriter wr = new XmlTextWriter(res); transform.Transform(xdoc, null, wr); wr.Close(); //return rng.WordOpenXML; //throw new NotImplementedException(); return ""; * */ string m_out = ""; return Trafo((string)m_App.ActiveDocument.WordOpenXML, (string)Properties.Resources.OpenXML,out m_out); } public string Trafo(string XMLPage, string XSLStylesheet, out string ErrMsg) { //the outputs string result=""; ErrMsg=""; try { XMLPage = XMLPage.Replace("<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">", "<Relationships>"); //read XML TextReader tr1=new StringReader(XMLPage); XmlTextReader tr11=new XmlTextReader(tr1); XPathDocument xPathDocument=new XPathDocument(tr11); //read XSLT TextReader tr2=new StringReader(XSLStylesheet); XmlTextReader tr22=new XmlTextReader(tr2); XslTransform xslt = new XslTransform (); xslt.Load(tr22); //create the output stream StringBuilder sb=new StringBuilder(); TextWriter tw=new StringWriter(sb); //xsl.Transform (doc, null, Console.Out); xslt.Transform(xPathDocument,null,tw); //get result result=sb.ToString(); } catch (Exception ex) { //Console.WriteLine (ex.Message); ErrMsg=ex.Message; } return result; }//Trafo public override string GetTextDocumentContent() { return m_App.ActiveDocument.Content.Text; } } }