コード例 #1
0
        public void Start(Book.Book book, CollectionSettings collectionSettings, Color backColor)
        {
            if (_wifiAdvertiser != null)
            {
                Stop();
            }

            // This listens for a BloomReader to request a book.
            // It requires a firewall hole allowing Bloom to receive messages on _portToListen.
            // We initialize it before starting the Advertiser to avoid any chance of a race condition
            // where a BloomReader manages to request an advertised book before we start the listener.
            _wifiListener = new BloomReaderUDPListener();
            _wifiListener.NewMessageReceived += (sender, args) =>
            {
                var json = Encoding.UTF8.GetString(args.Data);
                try
                {
                    dynamic settings = JsonConvert.DeserializeObject(json);
                    // The property names used here must match the ones in BloomReader, doInBackground method of SendMessage,
                    // a private class of NewBookListenerService.
                    var androidIpAddress = (string)settings.deviceAddress;

                    var androidName = (string)settings.deviceName;
                    // This prevents the device (or other devices) from queuing up requests while we're busy with this one.
                    // In effect, the Android is only allowed to request a retry after we've given up this try at sending.
                    // Of course, there are async effects from network latency. But if we do get another request while
                    // handling this one, we will ignore it, since StartSendBook checks for a transfer in progress.
                    _wifiAdvertiser.Paused = true;
                    StartSendBookOverWiFi(book, androidIpAddress, androidName, backColor);
                    // Returns immediately. But we don't resume advertisements until the async send completes.
                }
                // If there's something wrong with the JSON (maybe an obsolete or newer version of reader?)
                // just ignore the request.
                catch (Exception ex) when(ex is JsonReaderException || ex is JsonSerializationException)
                {
                    _progress.Error(id: "BadBookRequest",
                                    message: "Got a book request we could not process. Possibly the device is running an incompatible version of BloomReader?");

                    //this is too technical/hard to translate
                    _progress.ErrorWithoutLocalizing($" Request contains {json}; trying to interpret as JSON we got {ex.Message}");
                }
            };

            var pathHtmlFile = book.GetPathHtmlFile();

            _wifiAdvertiser = new WiFiAdvertiser(_progress)
            {
                BookTitle     = BookStorage.SanitizeNameForFileSystem(book.Title),             // must be the exact same name as the file we will send if requested
                TitleLanguage = collectionSettings.Language1Iso639Code,
                BookVersion   = Book.Book.MakeVersionCode(File.ReadAllText(pathHtmlFile), pathHtmlFile)
            };

            AndroidView.CheckBookLayout(book, _progress);
            _wifiAdvertiser.Start();

            _progress.Message(id: "WifiInstructions1",
                              message: "On the Android, run Bloom Reader, open the menu and choose 'Receive Books from computer'.");
            _progress.Message(id: "WifiInstructions2",
                              message: "You can do this on as many devices as you like. Make sure each device is connected to the same network as this computer.");
        }
コード例 #2
0
        /// <summary>
        /// This is our primary entry point for importing from a spreadsheet. There is also a CLI command,
        /// but we shouldn't need that one much, now that we have this on our book thumb context menus.
        /// </summary>
        public void HandleImportContentFromSpreadsheet(Book.Book book)
        {
            if (!_collectionModel.CollectionSettings.HaveEnterpriseFeatures)
            {
                Enterprise.ShowRequiresEnterpriseNotice(Form.ActiveForm, "Import to Spreadsheet");
                return;
            }

            var bookPath = book.GetPathHtmlFile();

            try
            {
                string inputFilepath;
                using (var dlg = new DialogAdapters.OpenFileDialogAdapter())
                {
                    dlg.Filter           = "xlsx|*.xlsx";
                    dlg.RestoreDirectory = true;
                    dlg.InitialDirectory = GetSpreadsheetFolderFor(book, false);
                    if (DialogResult.Cancel == dlg.ShowDialog())
                    {
                        return;
                    }
                    inputFilepath = dlg.FileName;
                    var spreadsheetFolder = Path.GetDirectoryName(inputFilepath);
                    SetSpreadsheetFolder(book, spreadsheetFolder);
                }

                var importer = new SpreadsheetImporter(_webSocketServer, book, Path.GetDirectoryName(inputFilepath));
                importer.ImportWithProgress(inputFilepath);

                // The importer now does BringBookUpToDate() which accomplishes everything this did,
                // plus it may have actually changed the folder (and subsequent 'bookPath')
                // due to a newly imported title. That would cause this call to fail.
                //XmlHtmlConverter.SaveDOMAsHtml5(book.OurHtmlDom.RawDom, bookPath);
                book.ReloadFromDisk(null);
                BookHistory.AddEvent(book, TeamCollection.BookHistoryEventType.ImportSpreadsheet);
                _bookSelection.InvokeSelectionChanged(false);
            }
            catch (Exception ex)
            {
                var msg = LocalizationManager.GetString("Spreadsheet:ImportFailed", "Import failed: ");
                NonFatalProblem.Report(ModalIf.All, PassiveIf.None, msg + ex.Message, exception: ex, showSendReport: false);
            }
        }
コード例 #3
0
 private void HandleExportToWord(Book.Book book)
 {
     try
     {
         MessageBox.Show(LocalizationManager.GetString("CollectionTab.BookMenu.ExportDocMessage",
                                                       "Bloom will now open this HTML document in your word processing program (normally Word or LibreOffice). You will be able to work with the text and images of this book. These programs normally don't do well with preserving the layout, so don't expect much."));
         var destPath = book.GetPathHtmlFile().Replace(".htm", ".doc");
         _collectionModel.ExportDocFormat(destPath);
         PathUtilities.OpenFileInApplication(destPath);
         Analytics.Track("Exported To Doc format");
     }
     catch (IOException error)
     {
         SIL.Reporting.ErrorReport.NotifyUserOfProblem(error.Message, "Could not export the book");
         Analytics.ReportException(error);
     }
     catch (Exception error)
     {
         SIL.Reporting.ErrorReport.NotifyUserOfProblem(error, "Could not export the book");
         Analytics.ReportException(error);
     }
 }