*&---------------------------------------------------------------------*
*&  Include           ZSOFTCOPY_DOWNLOAD                               *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*& This file is part of ZSOFTCOPY.                                     *
*&                                                                     *
*& ZSOFTCOPY is free software: you can redistribute it and/or modify   *
*& it under the terms of the GNU General Public License as published   *
*& by the Free Software Foundation, either version 3 of the License,   *
*& or any later version.                                               *
*&                                                                     *
*& ZSOFTCOPY is distributed in the hope that it will be useful,        *
*& but WITHOUT ANY WARRANTY; without even the implied warranty of      *
*& MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
*& GNU General Public License for more details.                        *
*&                                                                     *
*& You should have received a copy of the GNU General Public License   *
*& along with ZDOWNLOAD. If not, see <http://www.gnu.org/licenses/>.   *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&  Author:     Ruediger von Creytz     ruediger.creytz@globalbit.net  *
*&  Copyright:  globalBIT, LLC          http://www.globalbit.net       *
*&                                                                     *
*&---------------------------------------------------------------------*


*-----------------------------------------------------------------------
* form: download
*-----------------------------------------------------------------------
FORM download
  USING
    it_html TYPE it_string
    value(i_pgmid)
    value(i_object)
    value(i_filename)
    value(i_extension)
    value(i_do_count).

  DATA:
  l_infoline TYPE syucomm,
  l_uri TYPE string,
  l_sub_uri TYPE char255,
*  l_dbl_delimiter type string,
*  l_cnt_downloads type string,
*  l_cnt_elems type string,
  l_txt TYPE string,
  l_message TYPE string,
  l_codepage(20) TYPE c.

  PERFORM download_get_params
    USING
      i_pgmid
      i_object
      i_filename
      i_extension
      i_do_count
    CHANGING
      l_uri
      l_sub_uri
      l_infoline.

  PERFORM download_replace_wildcards
    USING
      l_sub_uri
    CHANGING
      it_html.

*  if i_do_count = abap_true.
*    g_cnt_downloads = g_cnt_downloads + 1.
*  endif.
*
*  l_cnt_downloads = g_cnt_downloads.
*  condense l_cnt_downloads.
*  l_cnt_elems = g_cnt_elems.
*  condense l_cnt_elems.
*  perform get_message_text using 'DSVAS' '226'
*                               i_filename space space space
*                         changing l_message.
*  concatenate
*    l_cnt_downloads '/' l_cnt_elems '|'
*    l_message into l_infoline
*    separated by space.
*
*  concatenate g_folder g_c_delimiter g_pack g_c_delimiter into l_uri.
*  if not i_pgmid is initial and not i_object is initial.
*    concatenate l_uri i_pgmid '_' i_object g_c_delimiter into l_uri.
*  endif.
*  concatenate l_uri i_filename into l_uri.
*  if not i_extension is initial.
*    concatenate l_uri '.' i_extension into l_uri.
*  endif.
*  concatenate g_c_delimiter g_c_delimiter into l_dbl_delimiter.
*  perform replace_all
*    using '/' g_c_delimiter
*    changing l_uri.
*  perform replace_all
*    using '<' '-'
*    changing l_uri.
*  perform replace_all
*    using '>' '-'
*    changing l_uri.
*  while l_uri cs l_dbl_delimiter.
*    perform replace_all
*      using l_dbl_delimiter g_c_delimiter
*      changing l_uri.
*  endwhile.

  IF NOT i_extension = gc_extension_htm
  AND NOT i_extension = gc_extension_xml.
    l_codepage = '4110'.                                    "UTF-8
  ENDIF.

  IF g_has_download_status = abap_true.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename             = l_uri
        codepage             = l_codepage
        show_transfer_status = abap_false
      TABLES
        data_tab             = it_html
      EXCEPTIONS
        OTHERS               = 1.
  ELSE.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename = l_uri
        codepage = l_codepage
      TABLES
        data_tab = it_html
      EXCEPTIONS
        OTHERS   = 1.
  ENDIF.
  IF sy-subrc <> 0.
    CLEAR l_message.
    l_txt = sy-subrc.
    CONDENSE l_txt.
    CONCATENATE 'RC=' l_txt ',' INTO l_txt.
    PERFORM get_message_text USING 'MEREP_00' '245'
                                   l_txt space space space
                             CHANGING l_message.
    CONCATENATE l_message l_uri INTO l_message SEPARATED BY space.
    WRITE / l_message.
  ELSE.
    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
        text = l_infoline.
  ENDIF.

ENDFORM.                    "download


*-----------------------------------------------------------------------
* form: download_binary
*-----------------------------------------------------------------------
FORM download_binary
  USING
    it_content TYPE it_o2content_binary
    value(i_bin_filesize) TYPE i
    value(i_pgmid)
    value(i_object)
    value(i_filename)
    value(i_extension)
    value(i_do_count).

  DATA:
  l_infoline TYPE syucomm,
  l_uri TYPE string,
  l_sub_uri TYPE char255,
  l_txt TYPE string,
  l_message TYPE string.

  PERFORM download_get_params
    USING
      i_pgmid
      i_object
      i_filename
      i_extension
      i_do_count
    CHANGING
      l_uri
      l_sub_uri
      l_infoline.

  IF g_has_download_status = abap_true.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        bin_filesize         = i_bin_filesize
        filename             = l_uri
        filetype             = 'BIN'
        show_transfer_status = abap_false
      TABLES
        data_tab             = it_content
      EXCEPTIONS
        OTHERS               = 2.
  ELSE.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        bin_filesize = i_bin_filesize
        filename     = l_uri
        filetype     = 'BIN'
      TABLES
        data_tab     = it_content
      EXCEPTIONS
        OTHERS       = 2.
  ENDIF.
  IF sy-subrc <> 0.
    CLEAR l_message.
    l_txt = sy-subrc.
    CONDENSE l_txt.
    CONCATENATE 'RC=' l_txt ',' INTO l_txt.
    PERFORM get_message_text USING 'MEREP_00' '245'
                                   l_txt space space space
                             CHANGING l_message.
    CONCATENATE l_message l_uri INTO l_message SEPARATED BY space.
    WRITE / l_message.
  ELSE.
    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
        text = l_infoline.
  ENDIF.

ENDFORM.                    "download_binary


*-----------------------------------------------------------------------
* form: download_get_params
*-----------------------------------------------------------------------
FORM download_get_params
  USING
    value(i_pgmid)
    value(i_object)
    value(i_filename)
    value(i_extension)
    value(i_do_count)
  CHANGING
    c_uri TYPE string
    c_sub_uri TYPE char255
    c_infoline TYPE syucomm.

  DATA:
  l_cnt_downloads TYPE string,
  l_cnt_elems TYPE string,
  l_txt TYPE string,
  l_message TYPE string,
  l_codepage(20) TYPE c.

  CLEAR:
  c_uri,
  c_sub_uri,
  c_infoline.

  IF i_do_count = abap_true.
    g_cnt_downloads = g_cnt_downloads + 1.
  ENDIF.

* new text for infoline
  l_cnt_downloads = g_cnt_downloads.
  CONDENSE l_cnt_downloads.
  l_cnt_elems = g_cnt_elems.
  CONDENSE l_cnt_elems.
  PERFORM get_message_text USING 'DSVAS' '226'
                               i_filename space space space
                         CHANGING l_message.
  CONCATENATE
    l_cnt_downloads '/' l_cnt_elems '|'
    l_message INTO c_infoline
    SEPARATED BY space.



* uri from top folder to file
  IF NOT i_pgmid IS INITIAL AND NOT i_object IS INITIAL.
    CONCATENATE c_uri i_pgmid '_' i_object gc_delimiter INTO c_uri.
  ENDIF.
  CONCATENATE c_uri i_filename INTO c_uri.
  IF NOT i_extension IS INITIAL.
    CONCATENATE c_uri '.' i_extension INTO c_uri.
  ENDIF.

  PERFORM download_make_plain_uri CHANGING c_uri.

  c_sub_uri = c_uri.
  IF c_sub_uri+0(1) = gc_delimiter.
    c_sub_uri = c_sub_uri+1.
  ENDIF.
  c_uri = c_sub_uri.

* full uri
  CONCATENATE g_folder gc_delimiter g_pack gc_delimiter c_uri
      INTO c_uri.
  PERFORM download_make_plain_uri CHANGING c_uri.

ENDFORM.                    "download_get_params


*-----------------------------------------------------------------------
* form: download_make_plain_uri
*-----------------------------------------------------------------------
FORM download_make_plain_uri
  CHANGING
    c_uri TYPE string.

  DATA:
  l_dbl_delimiter TYPE string.

  CONCATENATE gc_delimiter gc_delimiter INTO l_dbl_delimiter.
  PERFORM replace_all
    USING '/' gc_delimiter
    CHANGING c_uri.
  PERFORM replace_all
    USING '<' '-'
    CHANGING c_uri.
  PERFORM replace_all
    USING '>' '-'
    CHANGING c_uri.
  WHILE c_uri CS l_dbl_delimiter.
    PERFORM replace_all
      USING l_dbl_delimiter gc_delimiter
      CHANGING c_uri.
  ENDWHILE.

ENDFORM.                    "download_replace_wildcards


*-----------------------------------------------------------------------
* form: download_replace_wildcards
*-----------------------------------------------------------------------
FORM download_replace_wildcards
  USING
    value(i_sub_uri) TYPE char255
  CHANGING
    ct_html TYPE it_string.

  DATA:
  l_index_htm TYPE char255,
  l_cnt TYPE i,
  l_len TYPE i.

  CONSTANTS:
  lc_replacements TYPE i VALUE 2.

  FIELD-SYMBOLS:
  <ls_html> TYPE string.

  l_len = strlen( i_sub_uri ).

* make replacement for ../index.htm
  CONCATENATE 'index.' gc_extension_htm
      INTO l_index_htm.
  DO l_len TIMES.
    IF i_sub_uri+l_cnt(1) = gc_delimiter.
      CONCATENATE '../' l_index_htm INTO l_index_htm.
    ENDIF.
    ADD 1 TO l_cnt.
  ENDDO.

* replace
  CLEAR l_cnt.
  LOOP AT ct_html ASSIGNING <ls_html>.
    IF <ls_html> CS '../index.htm'.
      PERFORM replace_all
        USING
          '##INDEX_HTM##'
          l_index_htm
        CHANGING
          <ls_html>.
      ADD 1 TO l_cnt.
    ENDIF.
    IF l_cnt = lc_replacements.
      EXIT.
    ENDIF.
  ENDLOOP.

ENDFORM.                    "download_replace_wildcards
